I have lots of different strings from various sources, and I need to check if any of the string could be a valid date.
The problem is that there is no way to check if a string is a valid date for the following examples:
(I've tried both native javascript and moment.js. Nothing to do.)
const moment = require('moment');
const expectedDateFormat = ['DD-MM-YYYY','YYYY-MM-DD', moment.ISO_8601];
const examples = ['2019-03-31','2018-04-09T12:37:28Z','2013-02-04T22:44:30.652Z','28-10-2019','HAVTD-123224','HAVLF200974','1st action required','"$1445":3','On the night of Thursday May 17 2018 at 20:52 the vehicle was travelling Nord-Wesr when the impact occurred','HAVLF200323','HAVLF201037','HAVTD200110'];
const isValidDate = (string) => {
const date = moment(string, expectedDateFormat)
return date.isValid()
}
console.log('-----TRUE-----');
for (var i = 0; i< examples.length; i++) {
if(i == 4)
console.log('-----FALSE-----');
console.log(`${examples[i]} //`, isValidDate(examples[i]));
}
The first 4 strings only are valid Date. You can try to adjust and play the expectedDateFormat
, it still won't work.
These are the actual result printed in the console:
-----TRUE-----
2019-03-31 // true
2018-04-09T12:37:28Z // true
2013-02-04T22:44:30.652Z // true
28-10-2019 // true
-----FALSE-----
HAVTD-123224 // false
HAVLF200974 // true
1st action required // true
"$1445":3 // true
On the night of Thursday May 17 2018 at 20:52 the vehicle was travelling Nord-Wesr when the impact occurred // true
HAVLF200323 // true
HAVLF201037 // true
HAVTD200110 // true
Try any javascript framework/function that you want, I challenge you to make this work.