Skip to content

Instantly share code, notes, and snippets.

@liamkernighan
Created May 25, 2020 10:20
Show Gist options
  • Save liamkernighan/918ad9d7c5511939b364af0a9b081a9c to your computer and use it in GitHub Desktop.
Save liamkernighan/918ad9d7c5511939b364af0a9b081a9c to your computer and use it in GitHub Desktop.
ISO8601 JS tester
constructor(props: object) {
super(props)
console.log(this.isISO8601('1'))
console.log(this.isISO8601('2020-08-01T00:00:00Z'))
console.log(this.isISO8601('2020-08-01T00:00:00.000345Z'))
console.log(this.isISO8601('2020-06-08T00:01:01+05:59'))
}
private isISO8601 = (input: string) => {
const pattern = /^([\+-]?\d{4}(?!\d{2}\b))((-?)((0[1-9]|1[0-2])(\3([12]\d|0[1-9]|3[01]))?|W([0-4]\d|5[0-2])(-?[1-7])?|(00[1-9]|0[1-9]\d|[12]\d{2}|3([0-5]\d|6[1-6])))([T\s]((([01]\d|2[0-3])((:?)[0-5]\d)?|24\:?00)([\.,]\d+(?!:))?)?(\17[0-5]\d([\.,]\d+)?)?([zZ]|([\+-])([01]\d|2[0-3]):?([0-5]\d)?)?)?)?$/
return input.length > 10 && input.length < 30 && pattern.test(input)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment