用于日期范围选择器快捷项,忽略具体时间,包含今日/昨日/本周/上周/本月/上月。
/**
* 今日
* @returns {Date[]}
*/
function today () {
const now = new Date()
return [now, now]
}
/**
* 昨日
* @returns {Date[]}
*/
function yesterday () {
const now = new Date()
const yesterday = new Date()
yesterday.setTime(now.getTime() - 24 * 60 * 60 * 1000)
return [yesterday, yesterday]
}
/**
* 本周
* @returns {Date[]}
*/
function thisWeek () {
const monday = new Date()
const now = new Date()
const todayOfWeek = now.getDay() || 7
monday.setTime(now.getTime() - (todayOfWeek - 1) * 24 * 60 * 60 * 1000)
return [monday, now]
}
/**
* 上周
* @returns {Date[]}
*/
function lastWeek () {
const monday = new Date()
const sunday = new Date()
const now = new Date()
const todayOfWeek = now.getDay() || 7
sunday.setTime(now.getTime() - todayOfWeek * 24 * 60 * 60 * 1000)
monday.setTime(sunday.getTime() - 6 * 24 * 60 * 60 * 1000)
return [monday, sunday]
}
/**
* 本月
* @returns {Date[]}
*/
function thisMonth () {
const firstDay = new Date()
const now = new Date()
firstDay.setDate(1)
return [firstDay, now]
}
/**
* 上月
* @returns {Date[]}
*/
function lastMonth () {
const lastDay = new Date()
lastDay.setDate(0)
const firstDay = new Date(lastDay)
firstDay.setDate(1)
return [firstDay, lastDay]
}