将datetime转换为添加+1个月的时间戳毫秒

时间:2017-07-12 07:54:30

标签: angularjs datetime highcharts

高图表未在IE和Safari中呈现,此问题的解决方案是将我的日期从API转换为milliseconds中的时间戳。 这是转换代码

var date = '2017-06-07 10:00:00'

var d = date.match(/(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/)
console.log(d)
var parsed = +(new Date(d[1], d[2], d[3], d[4], d[5], d[6]).getTime())
console.log("timestamp: " + parsed) // 1499414400000 ==> July 7, 2017 8:00:00 AM

但我总是得+1个月 这是一个例子 js fiddle

3 个答案:

答案 0 :(得分:1)

This is because the moth count start with zero you can see here the full explanation:

https://www.w3schools.com/jsref/jsref_getmonth.asp

答案 1 :(得分:1)

Hello Please check it out this can be your perfect solution

const date = '2017-06-07 10:00:00'  
const d = date.match(/(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/)
var dates = new Date(d[0])
const parsed = new Date(dates).getTime()
Highcharts.stockChart('container', {
    series: [{
        data: [[parsed, 10]]
    }]
});

答案 2 :(得分:0)

thnx to all, here is how I fix this.

var date = '2017-06-07 10:00:00'

var d = date.match(/(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/)
var parsed = new Date(d[1], d[2]-1, d[3], d[4], d[5], d[6]).getTime() //I added -1 for month

Highcharts.stockChart('container', {
series: [{
    data: [[parsed, 10]]
}]
});