0

Im trying to convert UTC date to the current date 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);
        }
evann
  • 93
  • 1
  • 13

2 Answers2

1

You are trying to change the value of the function Date.parse; you wrote:

Date.parse = result[i]['date']; 

You need to call this function to parse the date

Try

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

and assign the result of this call to some variable to hold the date.

Date.parse documentation from Mozilla.

Ray Toal
  • 86,166
  • 18
  • 182
  • 232
  • Thanks, but i'm not sure on what to do after I assign the result – evann Jun 25 '13 at 13:27
  • Oh, sorry, I don't know what you _want_ to do with it. I see that you are pushing prices to an array; is the array supposed to contain both dates and prices? – Ray Toal Jun 25 '13 at 21:58
  • I want two separate arrays, the problem is that the date and time on the x-axis of the chart is not correct. Im correctly getting the arrays in my console. – evann Jun 26 '13 at 13:23
0

Take a look at this answer

The Date.parse method is completely implementation dependent (new Date(string) is equivalent to Date.parse(string)).

I would recommend you to parse your date string manually, and use the Date constructor with the year, month and day arguments to avoid ambiguity:

// parse a date in yyyy-mm-dd format
function parseDate(input) {
  var parts = input.split('-');
  // new Date(year, month [, date [, hours[, minutes[, seconds[, ms]]]]])
  return new Date(parts[0], parts[1]-1, parts[2]); // months are 0-based
}
Community
  • 1
  • 1
nicosantangelo
  • 13,216
  • 3
  • 33
  • 47