Skip to content

Commit c271ef3

Browse files
Add 'yy' format (#61)
* Add 'yy' format * Fix 'yy' parsing --------- Co-authored-by: Kasper <[email protected]>
1 parent 6d5225a commit c271ef3

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

src/lib/parse.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ export function parse(str: string, tokens: FormatToken[], baseDate: Date | null)
5757
function parseToken(token: FormatToken) {
5858
if (typeof token === 'string') {
5959
parseString(token)
60+
} else if (token.id === 'yy') {
61+
const value = parseUint(/^[0-9]{2}/, 0, 99)
62+
if (value !== null) year = 2000 + value
6063
} else if (token.id === 'yyyy') {
6164
const value = parseUint(/^[0-9]{4}/, 0, 9999)
6265
if (value !== null) year = value
@@ -103,6 +106,10 @@ const ruleTokens: RuleToken[] = [
103106
id: 'yyyy',
104107
toString: (d: Date) => d.getFullYear().toString(),
105108
},
109+
{
110+
id: 'yy',
111+
toString: (d: Date) => d.getFullYear().toString().slice(-2),
112+
},
106113
{
107114
id: 'MM',
108115
toString: (d: Date) => twoDigit(d.getMonth() + 1),

src/routes/docs/+page.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ Example format string: `yyyy-MM-dd HH:mm:ss`
5050
| Pattern | Result example |
5151
| :------ | :------------- |
5252
| `yyyy` | 2021 |
53+
| `yy` | 21 |
5354
| `MM` | 12 |
5455
| `dd` | 31 |
5556
| `HH` | 23 |

0 commit comments

Comments
 (0)