llm-router / apps /web /src /lib /dates.ts
ClaraSu's picture
Upload folder using huggingface_hub
747e1c6 verified
Raw History Blame Contribute Delete
1.7 kB
export function getCurrentMonth(): string {
const now = new Date()
return `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, '0')}`
}
export function monthRange(month: string): { from: string; to: string } {
const parts = month.split('-')
const year = Number(parts[0])
const mon = Number(parts[1])
const from = `${year}-${String(mon).padStart(2, '0')}-01`
const lastDay = new Date(year, mon, 0).getDate()
const to = `${year}-${String(mon).padStart(2, '0')}-${String(lastDay).padStart(2, '0')}`
return { from, to }
}
export function formatMonth(month: string): string {
const parts = month.split('-')
const year = Number(parts[0])
const mon = Number(parts[1])
return new Date(year, mon - 1, 1).toLocaleString('default', { month: 'long', year: 'numeric' })
}
export function getLast12Months(): string[] {
const months: string[] = []
const now = new Date()
for (let i = 0; i < 12; i++) {
const d = new Date(now.getFullYear(), now.getMonth() - i, 1)
months.push(`${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}`)
}
return months
}
// Takes a YYYY-MM-DD date string and returns its full weekday name (e.g.
// "Sunday"). A bare calendar date's weekday is a timezone-independent fact —
// this avoids the classic new Date("2026-08-09") UTC-parse-vs-local-read
// mismatch by constructing AND reading the Date in local time throughout, so
// it can never disagree with itself no matter what timezone the browser is in.
export function weekdayOf(dateStr: string): string {
const [year, month, day] = dateStr.split('-').map(Number)
return new Date(year!, month! - 1, day!).toLocaleDateString('en-US', { weekday: 'long' })
}