web-base-admin/src/utils/date.js

25 lines
813 B
JavaScript
Raw Normal View History

2025-04-17 16:55:50 +08:00
import dayjs from 'dayjs'
export function getBeforeDays(n = 1) {
return dayjs().subtract(n, 'day').format('YYYY-MM-DD')
}
// 获取本月第一天
export function getMonthFirstDay() {
return dayjs().startOf('month').format('YYYY-MM-DD')
}
// 获取本月最后一天
export function getMonthLastDay() {
return dayjs().endOf('month').format('YYYY-MM-DD')
}
// 获取本月第一天和当前天前一天的日期,如果当天为本月第一天,则返回上月第一天和上月最后一天
export function getMonthFirstDayAndBeforeDay() {
const isFirstDate = new Date().getDate() === 1
if (isFirstDate) {
return [dayjs().add(-1, 'month').startOf('month').format('YYYY-MM-DD'), dayjs().add(-1, 'month').endOf('month').format('YYYY-MM-DD')]
}
return [getMonthFirstDay(), getBeforeDays(1)]
}