I want to validate the date- time format which is like '2014-08-29T06:44:03Z' for this i am looking for a reg ex.
Tried few combinations but those did not work for me.
Times are expressed in UTC (Coordinated Universal Time), with a special UTC designator ("Z"). Times are expressed in local time, together with a time zone offset in hours and minutes. A time zone offset of "+hh:mm" indicates that the date/time uses a local time zone which is "hh" hours and "mm" minutes ahead of UTC.
A regular expression for dates (YYYY-MM-DD) should check for 4 digits at the front of the expression, followed by a hyphen, then a two-digit month between 01 and 12, followed by another hyphen, and finally a two-digit day between 01 and 31.
Try this regex
\b[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z\b
There:
\b
- word boundary, to check what for example '92014-08-29T06:44:03Z' is invalid
[0-9]{n}
- match number with n digits
If a string must contain only date-time and no other chars, then use:
^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z$
The following regex accepts an ISO-8601 UTC date time string ensuring:
format: YYYY-MM-DDThh:mm:ssZ
example: 2016-07-08T12:30:00Z
where:
YYYY = 0000 to 9999
MM = 01 to 12
DD = 01 to 31
hh = 00 to 23
mm = 00 to 59
ss = 00 to 59
\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[1-2]\d|3[0-1])T(?:[0-1]\d|2[0-3]):[0-5]\d:[0-5]\dZ
You can test it out here: https://regex101.com/r/jE4cE4/1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With