evann
evann

Reputation: 93

How to convert a string to a javascript date object?

Im want to convert a UTC date and time string to the current time for the x axis on a chart. I'm pretty sure I'm not using Date.parse correctly. Any help is appreciated. Thanks.

$.ajax({
    url: "/chart/ajax_get_chart", // the URL of the controller action method
    dataType: "json",
    type: "GET",
    success: function (result) {
        var result = JSON.parse(result);
        series = [];
        for (var i = 0; i < result.length; i++) {
          date = Date.parse(result[i]['date']); 
          tempArray = [parseFloat(result[i]['price'])];
          series.push(tempArray);
          series.push(date);
        }

Upvotes: 2

Views: 131

Answers (1)

SLaks
SLaks

Reputation: 887275

Date.parse(result[i]['date']); 

You just parsed the date into a return value, then completely ignored the result.

You need to do something with the return value.

Also, Date.parse() returns a UNIX timestamp.
To create a Date instance, call new Date(str)

Upvotes: 9

Related Questions