在 JS 中,new Date 函数,可以传递进去一个时间戳,返回的时候时间戳对应的时间,也可以传递进去一个日期,得到时间戳。
该方法用的就是这个。
var now = new Date();
var month = now.getMonth() + 1;//js获取到的是月份是 0-11 所以要加1
var year = now.getFullYear();
var nextMonthFirstDay = new Date([year,month + 1,1].join('-')).getTime();
var oneDay = 1000 * 24 * 60 * 60;
var monthLast = new Date(nextMonthFirstDay - oneDay).getDate()
console.log([year,month,1].join('-'))
console.log([year,month,monthLast].join('-'))
now 当前时间
month 当前月份
year 当前年份
nextMonthFirstDay 下月第一天的时间戳
利用下月第一天的时间戳,减去一天的时间戳,得到的是上月最后一天时间戳