1、获取本周的星期一和星期天的日期
var now = new Date(); var nowTime = now.getTime(); var day=now.getDay();//当前星期几 var oneDayLong = 24*60*60*1000; var MondayTime = nowTime - (day-1)*oneDayLong; var SundayTime = nowTime + (7-day)*oneDayLong; var monday = new Date(MondayTime); var sunday = new Date(SundayTime); console.log(day) //星期一的时间戳 console.log(monday) //格式化星期一时间
2、获取近n个月的日期
比如求近3个月的日期,如果当前时间是2017-11-16,近3个月就是2017-09-01 00:00:00;
再比如求近6个月,如果当前时间是2017-3-16,近6个月就是2016-10-01 00:00:00;
function padZero(value){ //补0 return value<10 ? '0'+value:value; } // value为n,表示近几个月 function getTime(value){ var obj={}; var date = new Date(); obj.year=date.getFullYear(); obj.month=date.getMonth() + 1; var startStr=" "+"00"+":"+"00"+":"+"00"; if(value===12){
var startTime=obj.year+ "-" + 01 + "-" +01+startStr;
}else{ var val=obj.month-value*1+1; var startYear =val<0? obj.year-1:obj.year; var startMonth=val<0? cur+12:val; var startTime=startYear+ "-" + padZero(startMonth) + "-" +01+startStr } }