2

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);
        }
evann
  • 93
  • 1
  • 13
  • Can you show us the content of `result[i]['date']` and is there any error message? – Martin Lantzsch Jun 25 '13 at 18:55
  • 3
    You are not assigning the `parse()` result to anything. – acdcjunior Jun 25 '13 at 18:55
  • @acdcjunior I figured that was the problem, but I can't seem to get the right syntax to assign it, I'm new to javascript – evann Jun 25 '13 at 18:59
  • What do you want to do, really? To assign it to a variable is simple, but we need to know what is it you really need to guide you in the right direction. – acdcjunior Jun 25 '13 at 19:04
  • I'm guessing this has never been asked before: http://stackoverflow.com/questions/4048204/javascript-equivalent-of-phps-strtotime – Mulan Jun 25 '13 at 19:05
  • You could put them in the same array: `tempArray = [Date.parse(result[i]['date']), parseFloat(result[i]['price'])];` – Balint Bako Jun 25 '13 at 19:06
  • @acdcjunior I have a chart of bitcoin prices that updates live every time you refresh the page. Im getting the correct UTC in my console, but on the x axis of the chart it comes up as every other day in the month of January. I want the x-axis to have the current time up to the second. – evann Jun 25 '13 at 19:14

1 Answers1

9
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)

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • I'm not exactly sure what to do with the return value, I'm new to javascript. – evann Jun 25 '13 at 19:04
  • Just to make it a bit clearer: `x = Date.parse( datestring )` will parse a string as "Sun June 7 2013 10:00:00" or any other format to a UNIX timestamp (int) and asign it to x. `x = new Date( datestring )` will call the constructor with above string and assign a javascript Date object to x. You can use the Date functions, like `x.getTime()` to return the timestamp you would get with `parse` too or for example `x.getDate()` to get the day of the month. – Sumurai8 Jun 25 '13 at 19:04
  • @evann: What don't you understand? You probably want to put it in the array. – SLaks Jun 25 '13 at 21:26