document.write("");

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, "/")));

posted @ 2022-11-27 22:22  人间春风意  阅读(4585)  评论(0)    收藏  举报