[英]Convert string to javascript date object
Im trying to convert UTC date to the current date for the x axis on a chart. 我正在尝试将UTC日期转换为图表上x轴的当前日期。 I'm pretty sure I'm not using
Date.parse
correctly. 我敢肯定我没有正确使用
Date.parse
。 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);
}
You are trying to change the value of the function Date.parse
; 您正在尝试更改函数
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 . 来自Mozilla的Date.parse文档 。
Take a look at this answer 看看这个答案
The
Date.parse
method is completely implementation dependent (new Date(string)
is equivalent toDate.parse(string)
).Date.parse
方法完全依赖于实现 (new Date(string)
等效于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:
我建议您手动解析日期字符串,并将Date构造函数与年,月和日参数一起使用以避免歧义:
// 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 }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.