Converting an array of strings to an array of dateTimes

Mike J

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.

Igal S.

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.

edited at
0

Comments

0 comments
Login to comment

Related

Converting a Java ArrayList of strings to a JavaScript array

Converting strings in an array to words in a 2d array

Idiomatic way of converting array of strings to array of byte arrays

Python - converting strings in an array to dates

Python matplotlib.dates.date2num: converting numpy array to matplotlib datetimes

C Converting Char array (strings) into int array

Converting an array of strings to an array of concatenated strings

Converting numpy array of strings to datetime

converting array of strings to single row values

Converting a JSON strings list to a JSON array in Python

Converting an array of strings containing range of integer values into an array of floats

Package for converting advanced search strings into assoc array?

Converting array of data to strings

Converting Array of Strings to floats (?!?!)

converting array to strings in sentence

Reading Strings from a file to an ArrayList then converting to an Array

Converting array of Strings to array of doubles

find in array of datetimes

Multidimensional array Converting inside arrays to strings

Converting an array of floats to an array of strings in scientific notation

Converting cell array of strings with number arrays to matrices

Converting Strings / ints into an Object Array

Converting string-array to an array of strings

Converting nested array elements to array of strings

Converting integer to array of strings

Converting an Array of Strings into key/values Array

Converting array of strings into array of objects in React JS

Numpy automatically converting array of strings to array of numbers

JavaScript - Converting string-array to an array of strings