Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regex to verify UTC date time format

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.

like image 925
Saurabh Singhai Avatar asked Aug 29 '14 12:08

Saurabh Singhai


People also ask

What format is UTC?

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.

Which of the following regular expression passes the calendar date?

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.


2 Answers

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$
like image 53
Rimas Avatar answered Sep 24 '22 13:09

Rimas


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

like image 37
Will Jones Avatar answered Sep 25 '22 13:09

Will Jones