Skip to content

Instantly share code, notes, and snippets.

@ppo
Last active September 10, 2024 15:36
Show Gist options
  • Save ppo/17bb97d14343d47efaf4 to your computer and use it in GitHub Desktop.
Save ppo/17bb97d14343d47efaf4 to your computer and use it in GitHub Desktop.
Validation Regex ECMA-262 5.1 (date and/or time with optional timezone)
Format: YYYY-MM-DDTHH:mm:ss.sssZ
Date: ^\d{4}(-\d{2}){0,2}$
Time: ^\d{2}(:\d{2}(:\d{2}(\.\d+)?)?)?$
Time with optional timezone: ^\d{2}(:\d{2}(:\d{2}(\.\d+)?)?)?(Z|[\+|-]\d{2}:\d{2})?$
Date and/or time with optional timezone:
(^\d{4}(-\d{2}){0,2})?((^|T)\d{2}(:\d{2}(:\d{2}(\.\d+)?)?)?(Z|[\+|-]\d{2}:\d{2})?)?$
# Must match
2014-12-31
2014-12
2014
23:45:59.123
23:45:59
23:45
2014-12-31T23:45:59.123
2014-12-31T23:45:59
2014-12-31T23:45
2014-12T23:45:59.123
2014-12T23:45:59
2014-12T23:45
2014T23:45:59.123
2014T23:45:59
2014T23:45
23:45:59.123Z
23:45:59.123+04:30
23:45:59.123-04:30
23:45:59Z
23:45:59+04:30
23:45:59-04:30
23:45Z
23:45+04:30
23:45-04:30
2014-12-31T23:45:59.123Z
2014-12-31T23:45:59.123+04:30
2014-12-31T23:45:59.123-04:30
2014-12-31T23:45:59Z
2014-12-31T23:45:59+04:30
2014-12-31T23:45:59-04:30
2014-12-31T23:45Z
2014-12-31T23:45+04:30
2014-12-31T23:45-04:30
2014-12T23:45:59.123Z
2014-12T23:45:59.123+04:30
2014-12T23:45:59.123-04:30
2014-12T23:45:59Z
2014-12T23:45:59+04:30
2014-12T23:45:59-04:30
2014-12T23:45Z
2014-12T23:45+04:30
2014-12T23:45-04:30
2014T23:45:59.123Z
2014T23:45:59.123+04:30
2014T23:45:59.123-04:30
2014T23:45:59Z
2014T23:45:59+04:30
2014T23:45:59-04:30
2014T23:45Z
2014T23:45+04:30
2014T23:45-04:30
# Must not match
2014-12-31 23:45:59.123
14-12-31
23:45.1234
# Remarks
- The time regex is a bit more flexible, allowing any number of decimal digits for milliseconds. Strict version: ^\d{2}(:\d{2}(:\d{2}(\.{3})?)?)?$
# ECMA-262 5.1 -- Date Time String Format
ECMAScript defines a string interchange format for date-times based upon a simplification of the ISO 8601 Extended Format. The format is as follows: YYYY-MM-DDTHH:mm:ss.sssZ
Where the fields are as follows:
YYYY is the decimal digits of the year 0000 to 9999 in the Gregorian calendar.
- “-” (hyphen) appears literally twice in the string.
MM is the month of the year from 01 (January) to 12 (December).
DD is the day of the month from 01 to 31.
T “T” appears literally in the string, to indicate the beginning of the time element.
HH is the number of complete hours that have passed since midnight as two decimal digits from 00 to 24.
: “:” (colon) appears literally twice in the string.
mm is the number of complete minutes since the start of the hour as two decimal digits from 00 to 59.
ss is the number of complete seconds since the start of the minute as two decimal digits from 00 to 59.
. “.” (dot) appears literally in the string.
sss is the number of complete milliseconds since the start of the second as three decimal digits.
Z is the time zone offset specified as “Z” (for UTC) or either “+” or “-” followed by a time expression HH:mm
This format includes date-only forms:
YYYY
YYYY-MM
YYYY-MM-DD
It also includes “date-time” forms that consist of one of the above date-only forms immediately followed by one of the following time forms with an optional time zone offset appended:
THH:mm
THH:mm:ss
THH:mm:ss.sss
All numbers must be base 10. If the MM or DD fields are absent “01” is used as the value. If the HH, mm, or ss fields are absent “00” is used as the value and the value of an absent sss field is “000”. The value of an absent time zone offset is “Z”.
Illegal values (out-of-bounds as well as syntax errors) in a format string means that the format string is not a valid instance of this format.
NOTE 1 As every day both starts and ends with midnight, the two notations 00:00 and 24:00 are available to distinguish the two midnights that can be associated with one date. This means that the following two notations refer to exactly the same point in time: 1995-02-04T24:00 and 1995-02-05T00:00
NOTE 2 There exists no international standard that specifies abbreviations for civil time zones like CET, EST, etc. and sometimes the same abbreviation is even used for two very different time zones. For this reason, ISO 8601 and this format specifies numeric representations of date and time.
# References
ECMA-262 5.1: http://www.ecma-international.org/ecma-262/5.1/#sec-15.9.1.15
ECMA-262 6 (draft): https://people.mozilla.org/~jorendorff/es6-draft.html#sec-date-time-string-format
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment