Skip to content

Instantly share code, notes, and snippets.

@dustinknopoff
Created March 25, 2021 12:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dustinknopoff/94ecdd03a487d3f4185be25deb5cbffc to your computer and use it in GitHub Desktop.
Save dustinknopoff/94ecdd03a487d3f4185be25deb5cbffc to your computer and use it in GitHub Desktop.
type Day28 = '01'|'02'|'03'|'04'|'05'|'06'|'07'|'08'|'09'|10
|11|12|13|14|15|16|17|18|19|20
|21|22|23|24|25|26|27|28;
type Month = '01'|'02'|'03'|'04'|'05'|'06'|'07'|'08'|'09'|'10'|'11'|'12'
type Digit = 0|1|2|3|4|5|6|7|8|9;
type Year = `${19 | 20}${Digit}${Digit}`
type IsDivide4<S extends string | number> = `${S}` extends '0' | '4' | '8' ? true :
`${S}` extends `${number | ''}${`${0|2|4|6|8}${0|4|8}`|`${1|3|5|7|9}${2|6}`}` ? true : false;
type IsLeapYear<S extends string | number> =
`${S}` extends `${infer Hundreds}00` ?
IsDivide4<Hundreds>:
IsDivide4<S>;
type DateString =
|`${Year}-${'01'|'03'|'05'|'07'|'08'|'10'|'12'}-${Day28|29|30|31}`
|`${Year}-${'04'|'06'|'09'|'11'}-${Day28|29|30}`
|{[Y in Year]: `${Y}-02-${IsLeapYear<Y> extends true ? Day28 | 29 : Day28}`}[Year]
let dateStrings: DateString[] = [
'1900-01-01',
'1900-01-31',
'1900-12-31',
'1900-11-31',
'1900-11-30',
'1900-02-29',
'1904-02-29',
'1900-02-28',
'1901-02-28',
'1901-02-28',
'1901-02-29',
'2015-02-28',
'2016-02-29',
'2017-02-29',
'2018-02-29',
'2019-02-29',
'2020-02-28',
];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment