Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/trueberryless/the-thinking-hub. The central knowledge hub for 8 key Computer Science thinking modes. the-thinking-hub.felixs.dev
Something went wrong. Try again.
1.6 kB · 66 lines
TypeScript
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667import { themeConfig } from '@/config'import type { DateFormat } from '@/types'
const MONTHS_EN = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
const VALID_SEPARATORS = ['.', '-', '/']
/** * @param date * @param format * @returns */export function formatDate(date: Date, format?: string): string { const formatStr = (format || themeConfig.date.dateFormat).trim() const configSeparator = themeConfig.date.dateSeparator || '-'
const separator = VALID_SEPARATORS.includes(configSeparator.trim()) ? configSeparator.trim() : '.'
const year = date.getFullYear() const month = date.getMonth() + 1 const day = date.getDate() const monthName = MONTHS_EN[date.getMonth()]
const pad = (num: number) => String(num).padStart(2, '0')
switch (formatStr) { case 'YYYY-MM-DD': return `${year}${separator}${pad(month)}${separator}${pad(day)}`
case 'MM-DD-YYYY': return `${pad(month)}${separator}${pad(day)}${separator}${year}`
case 'DD-MM-YYYY': return `${pad(day)}${separator}${pad(month)}${separator}${year}`
case 'MONTH DAY YYYY': return `<span class="month">${monthName}</span> ${day} ${year}`
case 'DAY MONTH YYYY': return `${day} <span class="month">${monthName}</span> ${year}`
default: return `${year}${separator}${pad(month)}${separator}${pad(day)}` }}
export const SUPPORTED_DATE_FORMATS: readonly DateFormat[] = [ 'YYYY-MM-DD', 'MM-DD-YYYY', 'DD-MM-YYYY', 'MONTH DAY YYYY', 'DAY MONTH YYYY'] as const