(1)js 获取当前日期 前N天或者后N天日期 且可指定格式 如yyyy-mm-dd等

来源:互联网 发布:windows 10通怎么ping 编辑:程序博客网 时间:2024/05/16 20:36

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><HTML> <HEAD>  <TITLE> New Document </TITLE>  <META NAME="Generator" CONTENT="EditPlus">  <META NAME="Author" CONTENT="">  <META NAME="Keywords" CONTENT="">  <META NAME="Description" CONTENT=""> </HEAD><script type="text/javascript">/* * fomateDate Function */function fomateDate(oDate, sFomate, bZone) {sFomate = sFomate.replace("YYYY", oDate.getFullYear());  //将sFomate中的YYYY替换为oDate.getFullYear()sFomate = sFomate.replace("YY", String(oDate.getFullYear()).substr(2))//String(value)把给定的值转换成字符串。sFomate = sFomate.replace("MM",  oDate.getMonth()+1)//替换MMsFomate = sFomate.replace("DD",  oDate.getDate());//替换DDsFomate = sFomate.replace("hh", oDate.getHours());//替换hhsFomate = sFomate.replace("mm", oDate.getMinutes());//替换mmsFomate = sFomate.replace("ss", oDate.getSeconds());//替换ssif (bZone) sFomate = sFomate.replace(/\b(\d)\b/g, '0$1');//月份单位数x 替换为0X   (\b匹配一个单词的边界)return sFomate;}var today = new Date();  var start=new Date(today.getTime()- 31 * 24 * 3600 * 1000);//31天前  var end=new Date(today.getTime() -  1 * 24 * 3600 * 1000); //1天前 alert(fomateDate(start, 'YYYY-MM-DD',true)); alert(fomateDate(end,   'YYYY-MM-DD',true));alert(fomateDate(start, 'YYYY-MM-DD')); alert(fomateDate(end,   'YYYY-MM-DD'));</script> <BODY>   </BODY></HTML>


0 0
原创粉丝点击