增加utc日期的计算
This commit is contained in:
parent
d1257f0fe9
commit
973c83dc36
55
src/utils/utcdate.util.ts
Normal file
55
src/utils/utcdate.util.ts
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
export const ONE_DAY = 24 * 60 * 60 * 1000
|
||||||
|
|
||||||
|
// format the date to the format we want
|
||||||
|
export const formatDate = (date: Date): string => {
|
||||||
|
const year = date.getUTCFullYear()
|
||||||
|
const month = (date.getUTCMonth() + 1 + '').padStart(2, '0')
|
||||||
|
const day = (date.getUTCDate() + '').padStart(2, '0')
|
||||||
|
return `${year}${month}${day}`
|
||||||
|
}
|
||||||
|
|
||||||
|
// get formated datestring of yesterday
|
||||||
|
export const yesterday = (date?: Date) => {
|
||||||
|
date = date || new Date()
|
||||||
|
date.setUTCDate(date.getUTCDate() - 1)
|
||||||
|
return date
|
||||||
|
}
|
||||||
|
|
||||||
|
export const nextday = (date?: Date) => {
|
||||||
|
date = date || new Date()
|
||||||
|
date.setUTCDate(date.getUTCDate() + 1)
|
||||||
|
return date
|
||||||
|
}
|
||||||
|
|
||||||
|
// calc days between two Date
|
||||||
|
export function daysBetween(date1: Date, date2: Date) {
|
||||||
|
// hours*minutes*seconds*milliseconds
|
||||||
|
const diffInMs = Math.abs(date1.getTime() - date2.getTime())
|
||||||
|
const diffInDays = Math.round(diffInMs / ONE_DAY)
|
||||||
|
return diffInDays
|
||||||
|
}
|
||||||
|
|
||||||
|
// get begin of one day
|
||||||
|
export const getDayBegin = (date: Date): Date => {
|
||||||
|
const year = date.getUTCFullYear()
|
||||||
|
const month = date.getUTCMonth()
|
||||||
|
const day = date.getUTCDate()
|
||||||
|
return new Date(year, month, day)
|
||||||
|
}
|
||||||
|
|
||||||
|
// get begin of n day ago
|
||||||
|
export const getNDayAgo = (n: number, begin: boolean): Date => {
|
||||||
|
const date = new Date(Date.now() - n * ONE_DAY)
|
||||||
|
if (begin) {
|
||||||
|
return getDayBegin(date)
|
||||||
|
} else {
|
||||||
|
return date
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// get begin of this month
|
||||||
|
export const getMonthBegin = (date: Date): Date => {
|
||||||
|
const year = date.getUTCFullYear()
|
||||||
|
const month = date.getUTCMonth()
|
||||||
|
return new Date(year, month, 1)
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user