Files
simple-react-app-kit/packages/lexical/src/date-value.ts
T

30 lines
922 B
TypeScript
Raw Normal View History

export function parseISODate(value: string): Date | undefined {
if (!isISODate(value)) return undefined
const [year, month, day] = value.split("-").map(Number)
return new Date(year!, month! - 1, day)
}
export function toISODate(date: Date): string {
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}`
}
export function formatDate(date: Date, locale?: string): string {
return new Intl.DateTimeFormat(locale, { dateStyle: "long" }).format(date)
}
export function isISODate(value: string): boolean {
if (!/^\d{4}-\d{2}-\d{2}$/.test(value)) return false
const [year, month, day] = value.split("-").map(Number)
const date = new Date(year!, month! - 1, day)
return (
date.getFullYear() === year &&
date.getMonth() === month! - 1 &&
date.getDate() === day
)
}