Skip to content

Instantly share code, notes, and snippets.

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 muhammadghazali/572bde1e9468dfc9f93ef7d6cda21181 to your computer and use it in GitHub Desktop.
Save muhammadghazali/572bde1e9468dfc9f93ef7d6cda21181 to your computer and use it in GitHub Desktop.
Regex to test whether the decimal fraction seconds is found or not in ISO 8601 datetime string
// the regex is generated using https://txt2re.com
// Time Stamp 1
var re1='((?:2|1)\\d{3}(?:-|\\/)(?:(?:0[1-9])|(?:1[0-2]))(?:-|\\/)(?:(?:0[1-9])|(?:[1-2][0-9])|(?:3[0-1]))(?:T|\\s)(?:(?:[0-1][0-9])|(?:2[0-3])):(?:[0-5][0-9]):(?:[0-5][0-9]))';
// Any Single Character 1
var re2='(\\.)';
// Any Single Digit 1
var re3='(\\d)';
// Any Single Digit 2
var re4='(\\d)';
// Any Single Digit 3
var re5='(\\d)';
// Any Single Word Character (Not Whitespace) 1
var re6='(Z)';
var p = new RegExp(re1 + re2 + re3 + re4 + re5 + re6, ["i"]);
var txt = '2016-11-09T16:00:00Z';
// false
console.log('p.test(txt)', p.test(txt));
txt = '2016-11-09T16:00:00.000Z';
// true
console.log('p.test(txt)', p.test(txt));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment