Beginner javascript learner here, trying to convert an array of strings to an array of dateTimes using just vanilla.
Sample input:
["2018-07-16T14:28:03.123",
"2018-07-20T14:00:27.123",
"2018-07-26T13:31:12.123",
"2018-08-12T13:03:37.123",
"2018-09-24T12:35:46.123"]
Wanted output:
[2018-07-16T14:28:03.123,
2018-07-20T14:00:27.123,
2018-07-26T13:31:12.123,
2018-08-12T13:03:37.123,
2018-09-24T12:35:46.123]
My string values are always dateTime string representations, so I've been trying to leverage new Date(stringDateTime)
to convert each string to it's dateTime counterpart, but I'm running into trouble on how to navigate the array and build a new one with the appropriate dateTime data types.
Use the map function:
var datesStr = ["2018-07-16T14:28:03.123", "2018-07-20T14:00:27.123", "2018-07-26T13:31:12.123", "2018-08-12T13:03:37.123", "2018-09-24T12:35:46.123"]
var dates = datesStr.map(d => new Date(d));
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments