Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
        }
like image 246
evann Avatar asked Jun 29 '26 22:06

evann


1 Answers

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)

like image 65
SLaks Avatar answered Jul 02 '26 11:07

SLaks



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!