javascript获得给定日期的前一天的日期

 1 /**
 2 * 获得当前日期的前一天
 3 */
 4 function getYestoday(date){    
 5   var yesterday_milliseconds=date.getTime()-1000*60*60*24;//转换成毫秒后减去一天的毫秒数
 6 // var yesterday = new Date();     
 7 // yesterday.setTime(yesterday_milliseconds);     
 8 //           
 9 // var strYear = yesterday.getFullYear();  
10 // var strDay = yesterday.getDate();  
11 // var strMonth = yesterday.getMonth()+1;
12 // if(strMonth<10)  
13 // {  
14 //   strMonth="0"+strMonth;  
15 // }  
16    //datastr = strYear+"-"+strMonth+"-"+strDay;
17    return new Date(yesterday_milliseconds);
18 }

大家可根据上面方法扩展获得前一个月的日期、前一年的日期,被注释掉的部分可以转换成你想要的日期格式。

js日期的构造函数:

1 new Date() 
2 new Date(milliseconds) 
3 new Date(datestring) 
4 new Date(year, month) 
5 new Date(year, month, day) 
6 new Date(year, month, day, hours) 
7 new Date(year, month, day, hours, minutes) 
8 new Date(year, month, day, hours, minutes, seconds) 
9 new Date(year, month, day, hours, minutes, seconds, microseconds) 

具体方法可在这里http://blog.sina.com.cn/s/blog_6a0cd5e501011so7.html浏览

原文地址:https://www.cnblogs.com/smallrock/p/3529021.html