function padLeftZero(str) {
return ('00' + str).substr(str.length)
}
function formatDate(date, fmt) {
if (!date) {
return ''
}
if (/(y+)/.test(fmt)) {
fmt = fmt.replace(
RegExp.$1,
(date.getFullYear() + '').substr(4 - RegExp.$1.length)
)
}
const o = {
'M+': date.getMonth() + 1,
'd+': date.getDate(),
'h+': date.getHours(),
'm+': date.getMinutes(),
's+': date.getSeconds()
}
for (const k in o) {
if (new RegExp(`(${k})`).test(fmt)) {
const str = o[k] + ''
fmt = fmt.replace(
RegExp.$1,
RegExp.$1.length === 1 ? str : padLeftZero(str)
)
}
}
return fmt
}
export function getDateRange(e) {
let today = new Date();
let todays = new Date(); // 今日
let tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() - 1); //昨日
let startOfWeek = new Date(today.setDate(today.getDate() - today.getDay())); //本周的开始日期
let endOfWeek = new Date(today.setDate(today.getDate() - today.getDay() + 6)); //本周结束时间
let startOfLastWeek = new Date(startOfWeek);
startOfLastWeek.setDate(startOfLastWeek.getDate() - 7); //上周开始时间
let endOfLastWeek = new Date(endOfWeek);
endOfLastWeek.setDate(endOfLastWeek.getDate() - 7); // 上周结束时间
let Monthday = new Date();
let year = Monthday.getFullYear();
let month = Monthday.getMonth() + 1;
let daysInMonth = new Date(year, month, 0).getDate();
let thisStartDate = new Date(year, month - 1, 1) //本月开始时间
let thisEndDate = new Date(year, month - 1, daysInMonth) //本月结束时间
// let lastMonth = getThisMonth();
let lastStartDate = new Date(new Date(thisStartDate).setMonth(new Date(thisStartDate).getMonth() -
1)); //上月开始
let lastEndDate = new Date(new Date(thisEndDate).setMonth(new Date(thisEndDate).getMonth(), 0)); //上月结束
const yearStartDate = new Date(); // 获取今年第一天
yearStartDate.setDate(1);
yearStartDate.setMonth(0);
const yearEndDate = new Date();
yearEndDate.setFullYear(yearEndDate.getFullYear() + 1); // 最后一天
yearEndDate.setMonth(0); // 明年的0月,也就是对应到1月,是存在的哦,不是不存在的0
yearEndDate.setDate(0); // 明年的0日
const lastYearSta = new Date();
lastYearSta.setFullYear(lastYearSta.getFullYear() - 1); // 去年第一天
lastYearSta.setMonth(0); // 明年的0月,也就是对应到1月,是存在的哦,不是不存在的0
lastYearSta.setDate(1); // 明年的0日
const lastYearEnd = new Date();
lastYearEnd.setFullYear(lastYearEnd.getFullYear()); // 最后一天
lastYearEnd.setMonth(0); // 明年的0月,也就是对应到1月,是存在的哦,不是不存在的0
lastYearEnd.setDate(0); // 明年的0日
let dateObj = {}
const sta = 'yyyy-MM-dd 00:00:00'
const end = 'yyyy-MM-dd 23:59:59'
if (e == '昨日') {
dateObj = {
sta: formatDate(tomorrow, sta),
end: formatDate(tomorrow, end)
}
} else if (e == '今日') {
dateObj = {
sta: formatDate(todays, sta),
end: formatDate(todays, end)
}
} else if (e == '本周') {
dateObj = {
sta: formatDate(startOfWeek, sta),
end: formatDate(endOfWeek, end)
}
} else if (e == '上周') {
dateObj = {
sta: formatDate(startOfLastWeek, sta),
end: formatDate(endOfLastWeek, end)
}
} else if (e == '本月') {
dateObj = {
sta: formatDate(thisStartDate, sta),
end: formatDate(thisEndDate, end)
}
} else if (e == '上月') {
dateObj = {
sta: formatDate(lastStartDate, sta),
end: formatDate(lastEndDate, end)
}
} else if (e == '今年') {
dateObj = {
sta: formatDate(yearStartDate, sta),
end: formatDate(yearEndDate, end)
}
} else if (e == '去年') {
dateObj = {
sta: formatDate(lastYearSta, sta),
end: formatDate(lastYearEnd, end)
}
}
return {
dateObj
};
}
export default getDateRange
js封装当前日期+年月日+格式转换格式(YYYY-mm-dd hh:mm:ss)
于 2023-08-22 10:05:07 首次发布