|
@@ -0,0 +1,178 @@
|
|
|
|
+const weekdays = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];
|
|
|
|
+
|
|
|
|
+// 格式化日期为 YYYY-MM-DD
|
|
|
|
+export function formatToMonthDay(date) {
|
|
|
|
+ const month = String(date.getMonth() + 1).padStart(2, '0')
|
|
|
|
+ const day = String(date.getDate()).padStart(2, '0')
|
|
|
|
+ return `${month}月${day}日`
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * 根据传入时间获取星期几
|
|
|
|
+ * @param {Date} date
|
|
|
|
+ */
|
|
|
|
+export function getWeekday(date) {
|
|
|
|
+ return weekdays[date.getDay()]
|
|
|
|
+}
|
|
|
|
+/**
|
|
|
|
+ * 判断两个时间是否是同一天
|
|
|
|
+ * @param {Object} date1
|
|
|
|
+ * @param {Object} date2
|
|
|
|
+ */
|
|
|
|
+export function isSameDay(date1, date2) {
|
|
|
|
+ // 检查两个日期是否为同一天
|
|
|
|
+ return date1.getFullYear() === date2.getFullYear() &&
|
|
|
|
+ date1.getMonth() === date2.getMonth() &&
|
|
|
|
+ date1.getDate() === date2.getDate()
|
|
|
|
+}
|
|
|
|
+// 格式化日期为 YYYY-MM-DD
|
|
|
|
+const formatDate = (date) => {
|
|
|
|
+ const year = date.getFullYear()
|
|
|
|
+ const month = String(date.getMonth() + 1).padStart(2, '0')
|
|
|
|
+ const day = String(date.getDate()).padStart(2, '0')
|
|
|
|
+ return `${year}-${month}-${day}`
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * 根据传入时间, 获取传入时间的本周的起止日期(结束日期为昨天)
|
|
|
|
+ * @param {Date} inputDate 传入的时间
|
|
|
|
+ */
|
|
|
|
+export function getCurrentWeekDates(inputDate) {
|
|
|
|
+ const dayOfWeek = inputDate.getDay() // 0 (周日) 到 6 (周六)
|
|
|
|
+
|
|
|
|
+ // 计算到本周一的日期差
|
|
|
|
+ // 如果今天是周一(1),则 diffToMonday = 0
|
|
|
|
+ // 如果今天是周日(0),则 diffToMonday = 6
|
|
|
|
+ const diffToMonday = dayOfWeek === 0 ? 6 : dayOfWeek - 1
|
|
|
|
+
|
|
|
|
+ // 获取本周一(本周的第一天)
|
|
|
|
+ const monday = new Date(inputDate)
|
|
|
|
+ monday.setDate(inputDate.getDate() - diffToMonday)
|
|
|
|
+
|
|
|
|
+ // 获取昨天的日期
|
|
|
|
+ const yesterday = new Date(inputDate)
|
|
|
|
+ // yesterday.setDate(inputDate.getDate() - 1)
|
|
|
|
+
|
|
|
|
+ return {
|
|
|
|
+ start: formatDate(monday),
|
|
|
|
+ end: formatDate(yesterday)
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * 根据传入时间, 获取传入时间的上一周的起止日期
|
|
|
|
+ * @param {Date} inputDate 传入的时间
|
|
|
|
+ */
|
|
|
|
+export function getLastWeekDates(inputDate) {
|
|
|
|
+ const dayOfWeek = inputDate.getDay() // 0 (周日) 到 6 (周六)
|
|
|
|
+ // 计算到上周一的日期差
|
|
|
|
+ // 如果今天是周一(1),则diffToLastWeek = 1 + 7 = 8(回到上周一)
|
|
|
|
+ // 如果今天是周日(0),则diffToLastWeek = 0 + 7 + 1 = 8(回到上周一)
|
|
|
|
+ const diffToLastWeek = dayOfWeek === 0 ? 7 : dayOfWeek - 1 + 7
|
|
|
|
+
|
|
|
|
+ // 获取上周一(上周的第一天)
|
|
|
|
+ const lastMonday = new Date(inputDate)
|
|
|
|
+ lastMonday.setDate(inputDate.getDate() - diffToLastWeek)
|
|
|
|
+
|
|
|
|
+ // 获取上周日(上周的最后一天)
|
|
|
|
+ const lastSunday = new Date(lastMonday)
|
|
|
|
+ lastSunday.setDate(lastMonday.getDate() + 6)
|
|
|
|
+
|
|
|
|
+ return {
|
|
|
|
+ start: formatDate(lastMonday),
|
|
|
|
+ end: formatDate(lastSunday)
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * 根据传入时间, 获取传入时间的下一周的起止日期, 结束日期不能超过今天, 如果超过今天, 则结束日期是今天
|
|
|
|
+ * @param {Date} inputDate 传入的时间
|
|
|
|
+ */
|
|
|
|
+export function getNextWeekDates(inputDate) {
|
|
|
|
+ const dayOfWeek = inputDate.getDay() // 0 (周日) 到 6 (周六)
|
|
|
|
+
|
|
|
|
+ // 计算到下周一的日期差
|
|
|
|
+ // 如果今天是周一(1),则 diffToNextMonday = 7
|
|
|
|
+ // 如果今天是周日(0),则 diffToNextMonday = 1
|
|
|
|
+ const diffToNextMonday = dayOfWeek === 0 ? 1 : 7 - dayOfWeek + 1
|
|
|
|
+
|
|
|
|
+ // 获取下周一(下一周的第一天)
|
|
|
|
+ const nextMonday = new Date(inputDate)
|
|
|
|
+ nextMonday.setDate(inputDate.getDate() + diffToNextMonday)
|
|
|
|
+
|
|
|
|
+ // 获取下一周的结束日期(下周一加6天)
|
|
|
|
+ const nextSunday = new Date(nextMonday)
|
|
|
|
+ nextSunday.setDate(nextMonday.getDate() + 6)
|
|
|
|
+
|
|
|
|
+ // 确保结束日期不超过今天
|
|
|
|
+ const today = new Date()
|
|
|
|
+ today.setHours(0, 0, 0, 0) // 设置为今天的午夜,以便比较
|
|
|
|
+
|
|
|
|
+ if (nextSunday > today) {
|
|
|
|
+ nextSunday.setTime(today.getTime()) // 如果超过今天,则设置为今天
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return {
|
|
|
|
+ start: formatDate(nextMonday),
|
|
|
|
+ end: formatDate(nextSunday)
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * 根据传入时间, 获取传入时间的当月的起止日期(结束时间为当前日期)
|
|
|
|
+ * @param {Date} inputDate 传入的时间
|
|
|
|
+ */
|
|
|
|
+export function getMonthlyDates(inputDate) {
|
|
|
|
+ // 获取传入日期所在月份的第一天
|
|
|
|
+ const firstDayOfMonth = new Date(inputDate.getFullYear(), inputDate.getMonth(), 1)
|
|
|
|
+
|
|
|
|
+ // 获取昨天的日期
|
|
|
|
+ const yesterday = new Date(inputDate)
|
|
|
|
+ // yesterday.setDate(inputDate.getDate() - 1)
|
|
|
|
+
|
|
|
|
+ return {
|
|
|
|
+ start: formatDate(firstDayOfMonth),
|
|
|
|
+ end: formatDate(yesterday)
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * 根据传入时间, 获取当前时间的上月的起止日期
|
|
|
|
+ * @param {Date} inputDate 传入的时间
|
|
|
|
+ */
|
|
|
|
+export function getLastMonthDates(inputDate) {
|
|
|
|
+ // 获取传入日期所在月份的第一天
|
|
|
|
+ const firstDayOfCurrentMonth = new Date(inputDate.getFullYear(), inputDate.getMonth(), 1)
|
|
|
|
+
|
|
|
|
+ // 获取上个月的最后一天
|
|
|
|
+ const lastDayOfLastMonth = new Date(firstDayOfCurrentMonth - 1)
|
|
|
|
+
|
|
|
|
+ // 获取上个月的第一天
|
|
|
|
+ const firstDayOfLastMonth = new Date(lastDayOfLastMonth.getFullYear(), lastDayOfLastMonth.getMonth(), 1)
|
|
|
|
+
|
|
|
|
+ return {
|
|
|
|
+ start: formatDate(firstDayOfLastMonth),
|
|
|
|
+ end: formatDate(lastDayOfLastMonth)
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * 根据传入时间, 获取传入时间的下一月的起止日期, 结束日期不能超过今天, 如果超过今天, 则结束日期是今天
|
|
|
|
+ * @param {Date} inputDate 传入的时间
|
|
|
|
+ */
|
|
|
|
+export function getNextMonthDates(inputDate) {
|
|
|
|
+ // 获取下一月的第一天
|
|
|
|
+ const nextMonthFirstDay = new Date(inputDate.getFullYear(), inputDate.getMonth() + 1, 1)
|
|
|
|
+ // 获取下一月的最后一天
|
|
|
|
+ const nextMonthLastDay = new Date(nextMonthFirstDay.getFullYear(), nextMonthFirstDay.getMonth() + 1, 0)
|
|
|
|
+ // 确保结束日期不超过今天
|
|
|
|
+ const today = new Date()
|
|
|
|
+ today.setHours(0, 0, 0, 0) // 设置为今天的午夜,以便比较
|
|
|
|
+ if (nextMonthLastDay > today) {
|
|
|
|
+ nextMonthLastDay.setTime(today.getTime()) // 如果超过今天,则设置为今天
|
|
|
|
+ }
|
|
|
|
+ return {
|
|
|
|
+ start: formatDate(nextMonthFirstDay),
|
|
|
|
+ end: formatDate(nextMonthLastDay)
|
|
|
|
+ }
|
|
|
|
+}
|