js获取当前时间往前推一个月的时间
function getLastMonth(date) {
const now = new Date(date)
const year = now.getFullYear();
const month = now.getMonth() + 1;
const day = now.getDate();
const nowMonthDay = new Date(year, month, 0).getDate(); // 当前月的总天数
if (month - 1 <= 0) return year - 1 + "-" + 12 + "-" + day; // 如果是1月,年数往前推一年
const lastMonthDay = new Date(year, parseInt(month) - 1, 0).getDate();
if (lastMonthDay >= day) return year + "-" + (month -1) + "-" + day;
if (day < nowMonthDay) return year + "-" + (month - 1) + "-" + (lastMonthDay - (nowMonthDay - day)); // 1个月前所在月的总天数小于现在的天日期
return year + "-" + (month - 1) + "-" + lastMonthDay; // 当前天日期小于当前月总天数
}
new Date(new Date().toLocaleDateString()).getTime() - 31 * 24 * 3600 * 1000 //31 * 24 * 3600 * 1000 计算一个月的时间戳,如需计算一周,则改成 7 * 24 * 3600 * 1000
var s = "2005-12-15 09:41:30";
var d = new Date(Date.parse(s.replace(/-/g, "/")));
作者:人间春风意
扫描左侧的二维码可以赞赏

本作品采用署名-非商业性使用-禁止演绎 4.0 国际 进行许可。

浙公网安备 33010602011771号