一、当前时间
let printTime = new Date(new Date().getTime() + 8 * 60 * 60 * 1000)
.toISOString()
.replace(/T/, " ")
.replace(/\..+/, "")
.substring(0, 19);
二、当前月第一天和最后一天
function getCurrentMonth() {
const start = new Date();
const end = new Date();
const year = start.getFullYear();
const month = start.getMonth();
start.setDate(1);
start.setHours(0, 0, 0, 0);
end.setMonth(month + 1);
end.setDate(0);
end.setHours(23, 59, 59, 999);
return [start,end]
},
三、最近三个月的开始时间,结束时间
function getRecentThreeMonthsDateRange() {
const currentDate = new Date();
const threeMonthsAgoDate = new Date(currentDate);
threeMonthsAgoDate.setHours(0, 0, 0, 0);
threeMonthsAgoDate.setMonth(threeMonthsAgoDate.getMonth() - 3);
const startYear = threeMonthsAgoDate.getFullYear();
const startMonth = String(threeMonthsAgoDate.getMonth() + 1).padStart(2, '0');
const startDay = String(threeMonthsAgoDate.getDate()).padStart(2, '0');
const startHours = '00';
const startMinutes = '00';
const startSeconds = '00';
const startDate = `${startYear}-${startMonth}-${startDay} ${startHours}:${startMinutes}:${startSeconds}`;
const endYear = currentDate.getFullYear();
const endMonth = String(currentDate.getMonth() + 1).padStart(2, '0');
const endDay = String(currentDate.getDate()).padStart(2, '0');
const endHours = String(currentDate.getHours()).padStart(2, '0');
const endMinutes = String(currentDate.getMinutes()).padStart(2, '0');
const endSeconds = String(currentDate.getSeconds()).padStart(2, '0');
const endDate = `${endYear}-${endMonth}-${endDay} ${endHours}:${endMinutes}:${endSeconds}`;
return [startDate, endDate];
}
console.log(getRecentThreeMonthsDateRange());
四、开始结束日期平铺
function fillDates(start, end) {
const dates = [];
const currentDate = new Date(start);
end = new Date(end);
while (currentDate <= end) {
dates.push(currentDate.toISOString().split('T')[0]);
currentDate.setDate(currentDate.getDate() + 1);
}
return dates;
}
const givenDates = [
"2024-11-12",
"2024-11-15"
];
const filledDates = fillDates(givenDates[0], givenDates[1]);
console.log(filledDates);