function getLastMonth() {
var now = new Date();
var year = now.getFullYear();
var month = now.getMonth() + 1; //0-11表示1-12月
var day = now.getDate();
var hours = now.getHours();
var minutes = now.getMinutes();
var seconds = now.getSeconds();
var dateObj = {};
hours = hours < 10 ? '0' + hours: hours;
minutes = minutes < 10 ? '0' + minutes: minutes;
seconds = seconds < 10 ? '0' + seconds: seconds;
dateObj.now = year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
//当前月的总天数
var nowMonthDay = new Date(year, month, 0).getDate();
if (month - 1 <= 0) {
// 如果是1月,年数往前推一年
dateObj.last = year -1 + "-" + 12 + "-" + day;
} else {
var lastMonthDay = new Date(year, parseInt(month) - 1, 0).getDate();
if (lastMonthDay < day) {
// 1个月前所在月的总天数小于现在的天日期
if (day < nowMonthDay) {
// 当前天日期小于当前月总天数
dateObj.last = year + "-" + (month - 1) + "-" + (lastMonthDay - (nowMonthDay - day));
} else {
dateObj.last = year + "-" + (month - 1) + "-" + lastMonthDay;
}
} else {
dateObj.last = year + "-" + (month -1) + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
}
}
return dateObj;
}
js返回一个月前的日期时间
最新推荐文章于 2024-05-08 09:48:00 发布