简体   繁体   English

在 JavaScript 中将字符串数组转换为时间戳

[英]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()循环遍历数组并使用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]);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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