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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/pandajava/article/details/47977761

<!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>


猜你喜欢

转载自blog.csdn.net/pandajava/article/details/47977761
今日推荐