返回 marp
date.ts
根目录 / website / utils / date.ts
1 const monthNames = [
2 'January',
3 'February',
4 'March',
5 'April',
6 'May',
7 'June',
8 'July',
9 'August',
10 'September',
11 'October',
12 'November',
13 'December',
14 ] as const
15
16 const nth = (day) => {
17 if (day > 3 && day < 21) return `${day}th`
18
19 const firstPlace = day % 10
20 if (firstPlace === 1) return `${day}st`
21 if (firstPlace === 2) return `${day}nd`
22 if (firstPlace === 3) return `${day}rd`
23
24 return `${day}th`
25 }
26
27 export const formatDate = (date: Date) =>
28 `${monthNames[date.getMonth()]} ${nth(date.getDate())}, ${date.getFullYear()}`
29
30 export const formatDateShort = (date: Date) =>
31 `${date.getFullYear()}-${`${date.getMonth() + 1}`.padStart(
32 2,
33 '0'
34 )}-${`${date.getDate()}`.padStart(2, '0')}`
35
35 lines TYPESCRIPT