// 获取当前时间和一个月之前的时间 window.init_date = function () { var currentDate = new Date(); // 获取当前日期和时间 currentDate.setMonth(currentDate.getMonth() - 1); // 将月份减去1 // var oneMonthAgo = currentDate.toISOString(); // 使用 ISO 标准返回 Date 对象的字符串格式:2023-09-26T02:33:10.065Z console.log("当前:", getTime(new Date())); //2023-09-26 10:34:18 console.log("一个月前:", getTime(currentDate)); //2023-08-26 10:34:18 docCreatimeBegin = getTime(currentDate); docCreatimeEnd = getTime(new Date()); } window.getTime = function (time) { var year = time.getFullYear(); // 获取当前年份 var month = time.getMonth() + 1; // 获取当前月份(需要加1,因为月份从0开始计数) var day = time.getDate(); // 获取当前日期 // 格式化日期和时间 var formattedDate = year + "-" + addLeadingZero(month) + "-" + addLeadingZero(day); function addLeadingZero(number) { if (number < 10) { return "0" + number; } return number; } return formattedDate; }