<!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)//替换MM
sFomate = sFomate.replace("DD", oDate.getDate());//替换DD
sFomate = sFomate.replace("hh", oDate.getHours());//替换hh
sFomate = sFomate.replace("mm", oDate.getMinutes());//替换mm
sFomate = sFomate.replace("ss", oDate.getSeconds());//替换ss
if (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>
(1)js 获取当前日期 前N天或者后N天日期 且可指定格式 如yyyy-mm-dd等
最新推荐文章于 2024-11-29 11:57:47 发布