JSFiddle - React, Tailwind, and code Playground
by Or Gal
JavaScript
var parseTS=function(str){
// validate year as 4 digits, month as 01-12, and day as 01-31 and time as 01-23 00-59
if ((str = str.match (/^(\d{4})(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])([01]\d|2[0-3])([0-5]\d)$/))) {
// make a date
str[0] = new Date (+str[1], +str[2] - 1, +str[3], +str[4], +str[5]);
// check if month stayed the same (ie that day number is valid)
if (str[0].getMonth () === +str[2] - 1) {
return str[0];
}
}
return undefined;
};
console.log(parseTS('201501011645'));
//this will return undefined (illegal hour)
console.log(parseTS('201501012645'));