Skip to content

Instantly share code, notes, and snippets.

@slinkardbrandon
Created February 9, 2023 18:54
Show Gist options
  • Save slinkardbrandon/81270f7001c251dbd282f04d19cce313 to your computer and use it in GitHub Desktop.
Save slinkardbrandon/81270f7001c251dbd282f04d19cce313 to your computer and use it in GitHub Desktop.
Typescript ISO Date Type

ISODateTypes

Unfortunately this fails due to the type being too difficult for typescript to represent (which makes sense), but this was still a fun type.

type OneThroughSix = 1 | 2 | 3 | 4 | 5 | 6;
type OneThroughNine = OneThroughSix | 7 | 8 | 9;
type ZeroThroughNine = 0 | OneThroughNine;
type ISOYear = `19${ZeroThroughNine}${ZeroThroughNine}` | `20${ZeroThroughNine}${ZeroThroughNine}`;
type ISOMonth = `0${OneThroughNine}` | `10` | `11` | `12`;
type ISODay = `0${ZeroThroughNine}` | `1${ZeroThroughNine}` | `2${ZeroThroughNine}` | `30` | `31`;
type ISOHours = `0${OneThroughNine}` | `1${OneThroughNine}` | `20` | `21` | `22` | `23` | `24`;
type ISOSecondsAndMinutes =
| `0${ZeroThroughNine}`
| `1${ZeroThroughNine}`
| `2${ZeroThroughNine}`
| `3${ZeroThroughNine}`
| `4${ZeroThroughNine}`
| `5${ZeroThroughNine}`;
type ISOMilliseconds = `${ZeroThroughNine}${ZeroThroughNine}${ZeroThroughNine}`;
export type ISOTimeString =
`${ISOHours}:${ISOSecondsAndMinutes}:${ISOSecondsAndMinutes}.${ISOMilliseconds}`;
export type ISODateString = `${ISOYear}-${ISOMonth}-${ISODay}`;
export type ISODateTimeString = `${ISODateString}T${ISOTimeString}Z`;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment