简体   繁体   中英

Convert Array of String to timestamp, in JavaScript

I Have an array which is displaying the time values as a string. So What I need is that I want to convert the string values to timestamp values so that I can use these values to create a chart.

for example I have values in the array format as following :

["2018-12-05 17:10:32", "2018-12-05 17:10:32", "2018-12-05 17:10:32", "2018-12-05 17:10:32", "2018-12-05 17:10:32", "2018-12-05 17:10:32", "2018-12-05 17:10:32", "2018-12-05 17:10:32", "2018-12-05 17:10:32", "2018-12-05 17:10:32", "2018-12-05 17:10:32"]

So my question is that how can I covert the above array string values into a timestamp

Loop through the array and convert them to date using new Date()

 var a = ["2018-12-05 17:10:32", "2018-12-05 17:10:32", "2018-12-05 17:10:32", "2018-12-05 17:10:32", "2018-12-05 17:10:32", "2018-12-05 17:10:32", "2018-12-05 17:10:32", "2018-12-05 17:10:32", "2018-12-05 17:10:32", "2018-12-05 17:10:32", "2018-12-05 17:10:32"]; a.forEach((e) => { var date = new Date(e); console.log(date) })

You can loop through the array and convert the string to a date object.

var dates = ["2018-12-05 17:10:32", "2018-12-05 17:10:32", "2018-12-05 17:10:32", "2018-12-05 17:10:32", "2018-12-05 17:10:32", "2018-12-05 17:10:32", "2018-12-05 17:10:32", "2018-12-05 17:10:32", "2018-12-05 17:10:32", "2018-12-05 17:10:32", "2018-12-05 17:10:32"];
for(var i = 0; i < dates.length; i++){
    dates[i] = new Date(dates[i]);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM