js获取日期:前天、昨天、今天、明天、后天
来源:程序员人生 发布时间:2014-03-12 00:46:57 阅读次数:3288次
js获取日期:前天、昨天、今天、明天、后天、大后天的代码:
第一种方法:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>js获取日期:前天、昨天、今天、明天、后天 - Liehuo.Net</title>
</head>
<body>
<script language="JavaScript" type="text/javascript">
function GetDateStr(AddDayCount)
{
var dd = new Date();
dd.setDate(dd.getDate()+AddDayCount);//获取AddDayCount天后的日期
var y = dd.getYear();
var m = dd.getMonth()+1;//获取当前月份的日期
var d = dd.getDate();
return y+"-"+m+"-"+d;
}
document.write("前天:"+GetDateStr(-2));
document.write("<br />昨天:"+GetDateStr(-1));
document.write("<br />今天:"+GetDateStr(0));
document.write("<br />明天:"+GetDateStr(1));
document.write("<br />后天:"+GetDateStr(2));
document.write("<br />大后天:"+GetDateStr(3));
</script>
</body>
</html>
第二种方法:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>js获取日期:前天、昨天、今天、明天、后天 - 学院</title>
</head>
<body>
<script language="JavaScript" type="text/javascript">
Date.prototype.DateAdd = function(strInterval, Number)
{
var dtTmp = this;
switch (strInterval) {
case 's' :return new Date(Date.parse(dtTmp) + (1000 * Number));
case 'n' :return new Date(Date.parse(dtTmp) + (60000 * Number));
case 'h' :return new Date(Date.parse(dtTmp) + (3600000 * Number));
case 'd' :return new Date(Date.parse(dtTmp) + (86400000 * Number));
case 'w' :return new Date(Date.parse(dtTmp) + ((86400000 * 7) * Number));
case 'q' :return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()) + Number*3, dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds());
case 'm' :return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()) + Number, dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds());
case 'y' :return new Date((dtTmp.getFullYear() + Number), dtTmp.getMonth(), dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds());
}
}
function GetDateStr2(AddDayCount)
{
var dd = new Date();
ddd = dd.DateAdd('d',AddDayCount);//三天后
var y = ddd.getYear();
var m = ddd.getMonth()+1;//获取当前月
var d = ddd.getDate();
return y+"-"+m+"-"+d;
}
document.write("前天:"+GetDateStr2(-2));
document.write("<br />昨天:"+GetDateStr2(-1));
document.write("<br />今天:"+GetDateStr2(0));
document.write("<br />明天:"+GetDateStr2(1));
document.write("<br />后天:"+GetDateStr2(2));
document.write("<br />大后天:"+GetDateStr2(3));
</script>
</body>
</html>