I have several dates (strings) that I want to convert to date objects.
const parse = (val, fmt) =>
{
if (!val)
return;
const fn = (str, len) => val.substr(fmt.indexOf(str), len);
return new Date(
fn('YYYY', 4),
fn('MM', 2) - 1,
fn('DD', 2)
);
};
console.log(parse('05/11/1896', 'DD/MM/YYYY')); // 1896-11-05
console.log(parse('07-12-2000', 'DD-MM-YYYY')); // 2000-12-07
console.log(parse('07:12:2000', 'DD:MM:YYYY')); // 2000-12-07
console.log(parse('2017/06/3', 'YYYY/MM/DD')); // 2017-06-03
console.log(parse('2017-06-15', 'YYYY-MM-DD')); // 2017-06-15
console.log(parse('2015 06 25', 'YYYY MM DD')); // 2015-06-25
mdy. Can this be done?
I also want to support months or days without a leading zero; so 5/5/2021 instead of 05/05/2021.
Thanks.