js 获取上个月的最后一天

//获取上个月最后一天
date = new Date();
console.log(date);
date.setDate(0); //setDate(day) 方法用于设置一个月的某一天。day取1-31表示对应的天数,另外0 为上一个月的最后一天,-1 为上一个月最后一天之前的一天,32 为下个月的第一天或者第二天
console.log(date);
var year = date.getFullYear();
var month = date.getMonth()+1;
month = month > 9 ? month:'0'+month;
var day = date.getDate();  //getDate() 方法可返回月份的某一天。也就是当前date的当前天数
day = day > 9 ? day:'0'+day;
console.log("year:"+year+"month:"+month+"day:"+day)
console.log("---------------------我是分割线------------------------")
//获取当月第一天
date = new Date();
var year = date.getFullYear();
var month = date.getMonth()+1;
month = month > 9 ? month:'0'+month;
console.log("year:"+year+"month:"+month+"day:01")

转载于:https://www.cnblogs.com/wangylblog/p/13970540.html

原文地址:https://www.cnblogs.com/xiaonangua/p/14832018.html