- function getYestoday(date){
- var yesterday_milliseconds=date.getTime()-1000*60*60*24;
- var yesterday = new Date();
- yesterday.setTime(yesterday_milliseconds);
-
- var strYear = yesterday.getFullYear();
- var strDay = yesterday.getDate();
- var strMonth = yesterday.getMonth()+1;
- if(strMonth<10)
- {
- strMonth="0"+strMonth;
- }
- datastr = strYear+"-"+strMonth+"-"+strDay;
- return datastr;
- }
-
-
- function getLastMonthYestdy(date){
- var daysInMonth = new Array([0],[31],[28],[31],[30],[31],[30],[31],[31],[30],[31],[30],[31]);
- var strYear = date.getFullYear();
- var strDay = date.getDate();
- var strMonth = date.getMonth()+1;
- if(strYear%4 == 0 && strYear%100 != 0){
- daysInMonth[2] = 29;
- }
- if(strMonth - 1 == 0)
- {
- strYear -= 1;
- strMonth = 12;
- }
- else
- {
- strMonth -= 1;
- }
- strDay = daysInMonth[strMonth] >= strDay ? strDay : daysInMonth[strMonth];
- if(strMonth<10)
- {
- strMonth="0"+strMonth;
- }
- if(strDay<10)
- {
- strDay="0"+strDay;
- }
- datastr = strYear+"-"+strMonth+"-"+strDay;
- return datastr;
- }
-
-
- function getLastYearYestdy(date){
- var strYear = date.getFullYear() - 1;
- var strDay = date.getDate();
- var strMonth = date.getMonth()+1;
- if(strMonth<10)
- {
- strMonth="0"+strMonth;
- }
- if(strDay<10)
- {
- strDay="0"+strDay;
- }
- datastr = strYear+"-"+strMonth+"-"+strDay;
- return datastr;
- }
function getYestoday(date){
var yesterday_milliseconds=date.getTime()-1000*60*60*24;
var yesterday = new Date();
yesterday.setTime(yesterday_milliseconds);
var strYear = yesterday.getFullYear();
var strDay = yesterday.getDate();
var strMonth = yesterday.getMonth()+1;
if(strMonth<10)
{
strMonth="0"+strMonth;
}
datastr = strYear+"-"+strMonth+"-"+strDay;
return datastr;
}
//获得上个月在昨天这一天的日期
function getLastMonthYestdy(date){
var daysInMonth = new Array([0],[31],[28],[31],[30],[31],[30],[31],[31],[30],[31],[30],[31]);
var strYear = date.getFullYear();
var strDay = date.getDate();
var strMonth = date.getMonth()+1;
if(strYear%4 == 0 && strYear%100 != 0){
daysInMonth[2] = 29;
}
if(strMonth - 1 == 0)
{
strYear -= 1;
strMonth = 12;
}
else
{
strMonth -= 1;
}
strDay = daysInMonth[strMonth] >= strDay ? strDay : daysInMonth[strMonth];
if(strMonth<10)
{
strMonth="0"+strMonth;
}
if(strDay<10)
{
strDay="0"+strDay;
}
datastr = strYear+"-"+strMonth+"-"+strDay;
return datastr;
}
//获得上一年在昨天这一天的日期
function getLastYearYestdy(date){
var strYear = date.getFullYear() - 1;
var strDay = date.getDate();
var strMonth = date.getMonth()+1;
if(strMonth<10)
{
strMonth="0"+strMonth;
}
if(strDay<10)
{
strDay="0"+strDay;
}
datastr = strYear+"-"+strMonth+"-"+strDay;
return datastr;
}
//获得下个月在昨天这一天的日期
function getLastMonthYestdy2(date){
var daysInMonth = new Array([0],[31],[28],[31],[30],[31],[30],[31],[31],[30],[31],[30],[31]);
var strYear = date.getFullYear();
var strDay = date.getDate();
var strMonth = date.getMonth()+1;
if(strYear%4 == 0 && strYear%100 != 0){
daysInMonth[2] = 29;
}
if(strMonth + 1 == 13)
{
strYear += 1;
strMonth = 1;
}
else
{
strMonth += 1;
}
strDay = daysInMonth[strMonth] >= strDay ? strDay : daysInMonth[strMonth];
if(strMonth<10)
{
strMonth="0"+strMonth;
}
if(strDay<10)
{
strDay="0"+strDay;
}
datastr = strYear+"-"+strMonth+"-"+strDay;
return datastr;
}
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
- <title>js获取日期:前天、昨天、今天、明天、后天 - Liehuo.Nettitle>
- 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>