将UTC日期转换为实际日期和时间

时间:2013-06-27 15:38:54

标签: javascript ajax utc

我有比特币价格图表。我在Y轴上有正确的价格,但我无法在X轴上得到正确的时间。时间在我的控制台中以UTC格式显示。我每次迭代都会在系列中添加价格和日期。我需要获取该特定结果的日期并找到该年份,月份和日期,以便我可以将其设置为正确的格式。感谢任何帮助。

 $.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++) {
                tempArray = [parseFloat(result[i]['price'])];
                tempDate = Date.parse(result[i]['date']); 
                series.push(tempDate);
                series.push(tempArray);
              }

3 个答案:

答案 0 :(得分:1)

Date对象有UTC方法可供您从中获取月,日和年。

var date = new Date(parseInt(result[i]['date'],10)),
month = date.getUTCMonth(),
day = date.getUTCDate(),
year = date.getUTCFullYear();

答案 1 :(得分:0)

Date.parse返回一个数字,表示自1970年1月1日以来经过的毫秒数。 您可以使用它来创建新的Date对象:

...
var ms = Date.parse(result[i]['date'])
var date = new Date(ms)
var year = Date.getFullYear();
...

使用Date.getFullYear()或Date.getMonth()等日期方法构建String

答案 2 :(得分:0)

Javascript日期对象有几种方法可用于为您提供日期:

tmpMonth = tempDate.getMonth()+1;

d = tempDate.getFullYear()+“ - ”+ tmpMonth +“ - ”+ tmpDate.getDate();

getMonth()方法需要增加1.