[英]Convert string to javascript date object
我正在嘗試將UTC日期轉換為圖表上x軸的當前日期。 我敢肯定我沒有正確使用Date.parse
。 任何幫助表示贊賞。 謝謝。
$.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);
}
您正在嘗試更改函數Date.parse
的值; 你寫了:
Date.parse = result[i]['date'];
您需要調用此函數來解析日期
嘗試
Date.parse(result[i]['date'])
並將此調用的結果分配給某個變量以保存日期。
看看這個答案
Date.parse
方法完全依賴於實現 (new Date(string)
等效於Date.parse(string)
)。我建議您手動解析日期字符串,並將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.