
正文
javascript获得给定日期的前一天的日期
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
/**
* 获得当前日期的前一天
*/
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 new Date(yesterday_milliseconds);
18 }
大家可根据上面方法扩展获得前一个月的日期、前一年的日期,被注释掉的部分可以转换成你想要的日期格式。
js日期的构造函数:
new Date()
new Date(milliseconds)
new Date(datestring)
new Date(year, month)
new Date(year, month, day)
new Date(year, month, day, hours)
new Date(year, month, day, hours, minutes)
new Date(year, month, day, hours, minutes, seconds)
new Date(year, month, day, hours, minutes, seconds, microseconds)
具体方法可在这里http://blog.sina.com.cn/s/blog_6a0cd5e501011so7.html浏览






