创建time.js
// 获取指定日期的 00:00:00 时间
export function getStartDate(date) {
date = date || new Date();
date.setHours(0, 0, 0, 0);
return date;
}
// 获取指定日期的 23:59:59 时间
export function getEndDate(date) {
date = date || new Date();
date.setHours(23, 59, 59, 999);
return date;
}
// 获取本周的起始日期和结束日期
export function getCurrentWeek() {
const currentDate = new Date();
const currentDayOfWeek = currentDate.getDay() - 1; // 0: Sunday, 1: Monday, ...
const startDate = new Date(currentDate);
startDate.setDate(currentDate.getDate() - currentDayOfWeek);
const endDate = new Date(startDate);
endDate.setDate(startDate.getDate() + 6);
return [getStartDate(startDate), getEndDate(endDate)];
}
// 获取当月的起始日期和结束日期
export function getCurrentMonth() {
const currentDate = new Date();
const year = currentDate.getFullYear();
const month = currentDate.getMonth();
const startDate = new Date(year, month, 1);
const endDate = new Date(year, month + 1, 0);
return [getStartDate(startDate), getEndDate(endDate)];
}
// 另一个
export function formatDate(date) {
var year = date.getFullYear();
var month = (date.getMonth() + 1).toString().padStart(2, '0');
var day = date.getDate().toString().padStart(2, '0');
var hour = date.getHo