I recently needed to validate RFC3339 date-time formatted dates. RFC3339 format is used for just about everything on the Internet. Most APIs use it and it’s supported by OpenAPI (Swagger) and JSON Schema. It looks like this:
2018-01-29T20:36:30+00:00. The format is defined here.
Despite being used everywhere I couldn’t find a good way to validate date-times in the programming language I was working in. The built in DateTime object didn’t support all valid RFC3339 dates and every regular expression I found was incorrect in some way or another.
I ended up sitting down with the IETF memo and writing my own regex, so here it is.
The Regular Expression
Here is the regex in PCRE format:
The regex only validates the format as outlined in section 5.6. It doesn’t validate the restrictions outlined in section 5.7.