You can use the Date()
constructor to create a date object from a string. The string must be in a format recognized by the Date.parse()
method or in the ISO format: YYYY-MM-DDTHH:mm:ss.sssZ
. Here's an example:
index.tsx101 chars4 lines
This will output: 2021-09-28T12:00:00.000Z
.
If the string is not in the correct format or cannot be parsed, the date object will be invalid, and you can test for this using the isNaN()
function.
index.tsx139 chars6 lines
gistlibby LogSnag