dependabot/npm_and_yarn/katex-0.16.27
copilot/add-i18n-support-for-english
dependabot/npm_and_yarn/astro-5.16.4
dependabot/npm_and_yarn/katex-0.16.27
dependabot/npm_and_yarn/prettier-3.7.4
dependabot/npm_and_yarn/tailwindcss-4.1.16
dependabot/npm_and_yarn/tailwindcss/vite-4.1.16
dependabot/npm_and_yarn/typescript-eslint-8.46.2
dependabot/npm_and_yarn/typescript-eslint/parser-8.46.2
development
main
| 1 | import { SITE } from '@/config' |
| 2 | |
| 3 | const MONTHS_EN = [ |
| 4 | 'Jan', |
| 5 | 'Feb', |
| 6 | 'Mar', |
| 7 | 'Apr', |
| 8 | 'May', |
| 9 | 'Jun', |
| 10 | 'Jul', |
| 11 | 'Aug', |
| 12 | 'Sep', |
| 13 | 'Oct', |
| 14 | 'Nov', |
| 15 | 'Dec' |
| 16 | ] |
| 17 | |
| 18 | const VALID_SEPARATORS = ['.', '-', '/'] |
| 19 | |
| 20 | /** |
| 21 | * @param date |
| 22 | * @param format |
| 23 | * @returns |
| 24 | */ |
| 25 | export function formatDate(date: Date, format?: string): string { |
| 26 | const formatStr = (format || SITE.dateFormat).trim() |
| 27 | const configSeparator = SITE.dateSeparator || '-' |
| 28 | |
| 29 | const separator = VALID_SEPARATORS.includes(configSeparator.trim()) ? configSeparator.trim() : '.' |
| 30 | |
| 31 | const year = date.getFullYear() |
| 32 | const month = date.getMonth() + 1 |
| 33 | const day = date.getDate() |
| 34 | const monthName = MONTHS_EN[date.getMonth()] |
| 35 | |
| 36 | const pad = (num: number) => String(num).padStart(2, '0') |
| 37 | |
| 38 | switch (formatStr) { |
| 39 | case 'YYYY-MM-DD': |
| 40 | return `${year}${separator}${pad(month)}${separator}${pad(day)}` |
| 41 | |
| 42 | case 'MM-DD-YYYY': |
| 43 | return `${pad(month)}${separator}${pad(day)}${separator}${year}` |
| 44 | |
| 45 | case 'DD-MM-YYYY': |
| 46 | return `${pad(day)}${separator}${pad(month)}${separator}${year}` |
| 47 | |
| 48 | case 'MONTH DAY YYYY': |
| 49 | return `<span class="month">${monthName}</span> ${day} ${year}` |
| 50 | |
| 51 | case 'DAY MONTH YYYY': |
| 52 | return `${day} <span class="month">${monthName}</span> ${year}` |
| 53 | |
| 54 | default: |
| 55 | return `${year}${separator}${pad(month)}${separator}${pad(day)}` |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | export const SUPPORTED_DATE_FORMATS = [ |
| 60 | 'YYYY-MM-DD', |
| 61 | 'MM-DD-YYYY', |
| 62 | 'DD-MM-YYYY', |
| 63 | 'MONTH DAY YYYY', |
| 64 | 'DAY MONTH YYYY' |
| 65 | ] as const |
| 66 | |
| 67 | export type DateFormat = (typeof SUPPORTED_DATE_FORMATS)[number] |