Skip to content

Instantly share code, notes, and snippets.

@marcelotmelo
Last active March 7, 2021 07:01
Show Gist options
  • Save marcelotmelo/b67f58a08bee6c2468f8 to your computer and use it in GitHub Desktop.
Save marcelotmelo/b67f58a08bee6c2468f8 to your computer and use it in GitHub Desktop.
^([0-9]+)-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])[Tt]([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]|60)(\.[0-9]+)?(([Zz])|([\+|\-]([01][0-9]|2[0-3]):[0-5][0-9]))$
@jy95
Copy link

jy95 commented Mar 28, 2017

For guys that just want a smaller regex (just replaced your [0-9] by \d ) :

^(\d+)-(0[1-9]|1[012])-(0[1-9]|[12]\d|3[01])\s([01]\d|2[0-3]):([0-5]\d):([0-5]\d|60)(\.\d+)?(([Zz])|([\+|\-]([01]\d|2[0-3])))$

an example :

2016-02-28 16:41:41.090Z

@WonderDev21
Copy link

var pattern = "The server will be offline in 2019-04-28T06:14:50.142Z for updates that will last 30 minutes until 2019-04-28T06:44:50.142Z ",
regexp = new RegExp('^([0-9]+)-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])Tt:([0-5][0-9]):([0-5][0-9]|60)(.[0-9]+)?(([Zz])|(+|-:[0-5][0-9]))$'),
test = regexp.test(pattern);
alert(test + "");
//// false;//////

It seems it is incorrect.
Could you please explain me....

@webmaster128
Copy link

@heraclesdev, first of all, pattern is a very misleading name herer. A pettern is what you put as a string into new RegExp(...) Better call it searchstring.

You likely get false because "^" and "$" in the RFC3339 pattern mark beginning and end of the string. I.e. "2019-04-28T06:14:50.142Z" will match but "it is 2019-04-28T06:14:50.142Z" will not.

@Zaijo
Copy link

Zaijo commented Sep 13, 2017

Hi @jy95 ! Your shortened regex is not correct. It's missing "T" or "t" between date and time representation.

This is the corresponding part of grammar that says it:
date-time = full-date "T" full-time

https://www.ietf.org/rfc/rfc3339.txt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment