0

I'm working on an app that gets values from the query string. Because the value comes from the query string, I'm basically working with string values. I need to write a JavaScript function that determines if a string is a date/time in ISO-8601 format. One of my challenges is that my JavaScript code uses strict mode (i.e. 'use strict';). Currently, I'm trying the following:

var isValidDateTime = function(dateString) {
  var pattern = new RegExp('/^\s*(?:[+-]\d{6}|\d{4})-(?:(\d\d-\d\d)|(W\d\d$)|(W\d\d-\d)|(\d\d\d))((T| )(\d\d(:\d\d(:\d\d(\.\d+)?)?)?)?([\+\-]\d\d(?::?\d\d)?|\s*Z)?)?$/');
  if (pattern.text(dateString) === true) {
    return true;
  } else {
    return false;
  }
};

Unfortunately, this doesn't seem to work. I'm passing '2014-07-28T10:53:59Z' in as the dateString value for testing purposes. I'm guessing my regular expression is incorrect. However, I'm not sure which one to use. Does anyone know how I can validate whether a string is a valid iso-8601 date/time?

Thank you

user687554
  • 10,663
  • 25
  • 77
  • 138

0 Answers0