你可以使用JavaScript的Date对象来获取三个月前的时间,并使用toISOString()方法来格式化为指定的'yyyy-MM-dd HH:mm:ss'格式。

下面是一个示例代码:

var currentDate = new Date(); // 获取当前时间
var threeMonthsAgo = new Date(currentDate.getFullYear(), currentDate.getMonth() - 3, currentDate.getDate()); // 获取三个月前的时间

// 格式化为'yyyy-MM-dd HH:mm:ss'格式
var formattedDate = threeMonthsAgo.toISOString().slice(0, 19).replace('T', ' ');

console.log(formattedDate);

输出结果可能为:2022-06-24 12:34:56

请注意,以上代码中的格式化方法仅适用于ISO格式的日期字符串,如果你需要使用其他格式,你可以使用JavaScript的日期格式化库,如moment.js。

JavaScript获取三个月前时间并格式化为yyyy-MM-dd HH:mm:ss

原文地址: https://www.cveoy.top/t/topic/qr7m 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录