mirror of
https://github.com/AgentSeal/codeburn.git
synced 2026-05-20 17:47:19 +00:00
Add UTC-based date helpers for period calculations
This commit is contained in:
parent
d61ea56b3e
commit
e2ebdc92e4
1 changed files with 50 additions and 0 deletions
50
desktop/src/lib/dates.ts
Normal file
50
desktop/src/lib/dates.ts
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
const DAY_NAMES = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']
|
||||
const MONTH_NAMES = [
|
||||
'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
|
||||
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec',
|
||||
]
|
||||
|
||||
function pad2(n: number): string {
|
||||
return n < 10 ? `0${n}` : String(n)
|
||||
}
|
||||
|
||||
export function todayKey(): string {
|
||||
return formatDateKey(new Date())
|
||||
}
|
||||
|
||||
export function formatDateKey(d: Date): string {
|
||||
return `${d.getUTCFullYear()}-${pad2(d.getUTCMonth() + 1)}-${pad2(d.getUTCDate())}`
|
||||
}
|
||||
|
||||
export function addDays(d: Date, n: number): Date {
|
||||
const r = new Date(d.getTime())
|
||||
r.setUTCDate(r.getUTCDate() + n)
|
||||
return r
|
||||
}
|
||||
|
||||
export function startOfDay(d: Date): Date {
|
||||
return new Date(Date.UTC(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate()))
|
||||
}
|
||||
|
||||
export function prettyDate(ymd: string): string {
|
||||
const [y, m, d] = ymd.split('-').map(Number)
|
||||
const dt = new Date(Date.UTC(y, m - 1, d))
|
||||
return `${DAY_NAMES[dt.getUTCDay()]} ${MONTH_NAMES[dt.getUTCMonth()]} ${dt.getUTCDate()}`
|
||||
}
|
||||
|
||||
export function shortDate(ymd: string): string {
|
||||
const parts = ymd.split('-')
|
||||
return `${parts[1]}/${parts[2]}`
|
||||
}
|
||||
|
||||
export function firstOfMonth(d: Date): Date {
|
||||
return new Date(Date.UTC(d.getUTCFullYear(), d.getUTCMonth(), 1))
|
||||
}
|
||||
|
||||
export function daysInMonth(d: Date): number {
|
||||
return new Date(Date.UTC(d.getUTCFullYear(), d.getUTCMonth() + 1, 0)).getUTCDate()
|
||||
}
|
||||
|
||||
export function dayOfMonth(d: Date): number {
|
||||
return d.getUTCDate()
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue