I have a String of the Format "2001-08-22 03:04:05.000."
How can I create a date object from it? I've tried Date.parse, but this causes errors.
I have a String of the Format "2001-08-22 03:04:05.000."
How can I create a date object from it? I've tried Date.parse, but this causes errors.
const newDate = new Date('2001-08-22 03:04:05.000');
console.log(newDate, typeof newDate, newDate.getMonth());
// throws an error
const newDate1 = new Date('2001-08-22 03:04:05.000.');
console.log(newDate1, typeof newDate1, newDate1.getMonth());
Just have to place the string in the Date() and you'll have access to it as an object
However if the 2001-08-22 03:04:05.000.
<-- if that period isn't a typo then that might be why you're getting an error.
Edit: Also as the link says Date.parse()
is discouraged due to browser compatibility.
new Date('2001-08-22 03:04:05.000').getFullYear();
new Date('2001-08-22 03:04:05.000').getMonth();
new Date('2001-08-22 03:04:05.000').getDate();
new Date('2001-08-22 03:04:05.000').getHours();
new Date('2001-08-22 03:04:05.000').getMinutes();
new Date('2001-08-22 03:04:05.000').getSeconds();