获取今日零点时间戳
export function todayStartTimestamp() {
const timestamp = Math.floor(new Date(new Date().setHours(0, 0, 0, 0)).getTime() / 1000)
return timestamp
}
获取今日结束时间戳
export function todayEndTimestamp() {
const timestamp = Math.floor(new Date(new Date().setHours(23, 59, 59, 999)).getTime() / 1000)
return timestamp
}
获取昨日开始、结束时间戳
export function yesterdayTimestamp(num = 1) {
const MillisecondsADay = 24 * 60 * 60 * num
const timestamp = Math.floor(new Date(new Date().setHours(0, 0, 0, 0)).getTime() / 1000)
const yesterdayStartTime = timestamp - MillisecondsADay
const yesterdayEndTime = timestamp - 1
return [yesterdayStartTime, yesterdayEndTime]
}
本周开始时间戳
export function weekStartTimestamp() {
const MillisecondsADay = 24 * 60 * 60
const timestamp = Math.floor(new Date(new Date().setHours(0, 0, 0, 0)).getTime() / 1000)
const weekDay = new Date().getDay() === 0 ? (7 - 1) : (new Date().getDay() - 1)
const weekTimeStamp = timestamp - MillisecondsADay * weekDay
return weekTimeStamp
}
上周开始、结束时间戳
export function lastWeekTimetamp() {
const MillisecondsADay = 24 * 60 * 60
const timestamp = Math.floor(new Date(new Date().setHours(0, 0, 0, 0)).getTime() / 1000)
const weekDay = new Date().getDay() === 0 ? (7 - 1) : (new Date().getDay() - 1)
const weekTimeStamp = timestamp - MillisecondsADay * weekDay
const lastWeekStart = weekTimeStamp - MillisecondsADay * 7
const lastWeekEnd = weekTimeStamp - 1
return [lastWeekStart, lastWeekEnd]
}
当月开始时间戳
export function monthStartTimestamp() {
const date = new Date()
date.setDate(1)
date.setHours(0, 0, 0, 0)
const timeStamp = date.getTime() / 1000
return timeStamp
}
获取上月开始、结束时间戳
export function lastMonthTimetamp() {
const MillisecondsADay = 24 * 60 * 60
const date = new Date()
date.setDate(1)
date.setHours(0, 0, 0, 0)
const timeStamp = date.getTime() / 1000
const days = lastMonthDats()
const lastMonthStart = timeStamp - (MillisecondsADay * days)
const lastMonthEnd = timeStamp - 1
return [lastMonthStart, lastMonthEnd]
}
function lastMonthDats() {
const date = new Date()
const year = date.getFullYear()
let month = (date.getMonth() + 1) - 1
month = month || 12
const arr30 = [4, 6, 9, 11]
const arr31 = [1, 3, 5, 7, 8, 10, 12]
if (arr30.indexOf(month) !== -1) {
return 30
} else if (arr31.indexOf(month) !== -1) {
return 31
} else {
if (isRunYear(year)) {
return 29
} else {
return 28
}
}
}
function isRunYear(year) {
let flag = false
if (year % 4 === 0 && year % 100 !== 0 || year % 400 === 0) {
flag = true
}
return flag
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix