1

For highstock / highcharts I need the date format to be like this:

series: [{
    data: [[1331028000000, 5], [1331031600000, 6], [1331035200000, 4]]
}]

The first number in each array is the date-stamp, I think it is UTC. When I get the data that I need to put into the chart, I get it like this:

"2013-06-08T06:00:00.000-07:00"

My question is, what format is this? And how should I convert it to what I need for highstock / highcharts.

Adil Shaikh
  • 44,509
  • 17
  • 89
  • 111
Watson
  • 105
  • 6

3 Answers3

1

Just throw that string into a Date() function and you should get it back as a Javascript Date object.

var myDate = new Date('2013-06-08T06:00:00.000-07:00');

To get it back as a formal UNIX timestamp, you just need to convert it back to a number, and divide by 1000 (as there are a thousand ms in a second), but it looks like you're looking for the ms value, not a formal UNIX timestamp.

Easiest way would be to use the + prefix operator.

var myUnixTimestamp = +myDate;
// 1370696400000
Richard Neil Ilagan
  • 14,627
  • 5
  • 48
  • 66
  • @Watson ~ well, that's what your *ISO 8601* string says, so I'd be very worried if it didn't evaluate to that. – Richard Neil Ilagan Jun 13 '13 at 14:30
  • Is that line the same as var myUnixTimestamp = '' + myDate; ? – Watson Jun 13 '13 at 14:30
  • @Watson ~ no, there probably should be no space between `+` and `myDate`. Well, it should work with a space --- as the operator is probably defined --- I just find that it reads cleaner without. – Richard Neil Ilagan Jun 13 '13 at 14:31
  • To clarify concepts: classic Unix timestamps are in *seconds*. JavaScript uses *milliseconds*. Thus `1370696400000` is equivalent to the `1370696400` Unix timestamp. – Álvaro González Jun 13 '13 at 14:32
  • @Watson `+x` in _JavaScript_ will do a cast to _Number_. For a _Date_, it will get the value in _ms_ since the unix epoch. – Paul S. Jun 13 '13 at 14:32
  • What is going on there then? Its not like += – Watson Jun 13 '13 at 14:33
  • @Watson ~ in Javascript, the `+` operator has three purposes: `arithmetic addition` (e.g. `var foo = 1 + 2;`), `string concatenation` (e.g. `var foo = 'hello' + 'world';`) and `number casting` (e.g. `var foo = '123'; var bar = +foo;`). The use of `+=` is just a combination of operation and assignment. – Richard Neil Ilagan Jun 13 '13 at 14:36
  • 1
    It might be worth noting that `+'123' === 123`, `+'-123' === -123`, etc, but `+'abc'` is `NaN`, not `0`. – Paul S. Jun 13 '13 at 14:37
  • @PaulS. ~ as well as the difference in behavior between `+'123a'` and `parseInt('123a')`. – Richard Neil Ilagan Jun 13 '13 at 14:39
1
    var date = new Date("2013-06-08T06:00:00.000-07:00");
    console.log(date.getTime());
  //or console.log(date.valueOf());
Khanh TO
  • 48,509
  • 13
  • 99
  • 115
  • Er... Aren't you missing something? This code basically converts `1331028000000` to `1331028000000` ;-P – Álvaro González Jun 13 '13 at 14:37
  • @Álvaro G. Vicario: just a sample code. The point is using date.getTime() or date.valueOf() to convert to milli-seconds since 01/01/1970 (UTC time) – Khanh TO Jun 13 '13 at 14:40
  • 1
    @Watson: updated, you have already accepted the other answer. Maybe you can see it as reference for another solution. – Khanh TO Jun 13 '13 at 14:49
0

Its millisecond, try using below code.

var date = new Date(1331028000000);
console.log(date.toString())
Praveen
  • 55,303
  • 33
  • 133
  • 164
  • `Tue Mar 06 2012 11:00:00 GMT+0100 (Hora de verano romance)`... as [documented](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toString). – Álvaro González Jun 13 '13 at 14:33
  • [http://stackoverflow.com/questions/498578/how-can-i-convert-a-date-value-in-iso-8601-format-to-a-date-object-in-javascript](http://stackoverflow.com/questions/498578/how-can-i-convert-a-date-value-in-iso-8601-format-to-a-date-object-in-javascript) may also help you. – Praveen Jun 13 '13 at 14:44