maikelm
maikelm

Reputation: 403

Check if date match with format using momentjs

I try to check if a date match with format using momentjs. But I have error when date include time zone.

Example:

moment('Mon Nov 10 2016 14:53:17', 'ddd MMM DD YYYY HH:mm:ss', true).isValid() 

in this case the response is true without errors. but in this other case, I not found the correct date format to compare

moment('Mon Nov 10 2016 14:53:17 GMT-0500 (ECT)', '?????', true).isValid() 

Upvotes: 0

Views: 1147

Answers (1)

jonzee
jonzee

Reputation: 1609

You can split your string in GTM-.. place and take only part before. Below is example, split() just splitting the string and return array, so first part of string (before GTM) will be at zero index.

var data = 'Mon Nov 10 2016 14:53:17 GMT-0500 (ECT)';
moment(date.split(' GMT')[0], 'ddd MMM DD YYYY HH:mm:ss', true).isValid() 

It works for strings without GTM part too, because it will return just one-element array with whole string.

Upvotes: 2

Related Questions