我在网上搜索过,但我找不到任何满意的答案。
从字符串重构Javascript Date
对象的正确方法是什么,该字符串是使用Date
编码的另一个$.param()
对象的表示。请注意,我只能访问序列化的日期字符串。它正在查询字符串中传入。我需要重建一个Date
。我是Javascript的新手,所以,如果我遗漏任何非常基本的东西,请好好告诉我。
var dtObj = { startTime: new Date() };
var paramDtObj = $.param(dtObj);
console.log(paramDtObj);//startTime=Mon+May+05+2014+12%3A33%3A09+GMT-0700+(Pacific+Daylight+Time)
var serDtString = "Mon+May+05+2014+12%3A33%3A09+GMT-0700+(Pacific+Daylight+Time)";
var reconstructedDt1 = new Date(serDtString);
console.log(reconstructedDt1);//Invalid Date
var reconstructedDt2 = new Date(decodeURIComponent(serDtString));
console.log(reconstructedDt2);//Invalid Date
var reconstructedDt3 = new Date(decodeURIComponent(serDtString).replace(/\+/g, " "));
console.log(reconstructedDt3);//WORKS. Output is - Mon May 05 2014 12:33:09 GMT-0700 (Pacific Daylight Time) - But this seems like a hack. What if it was GMT+ something instead of - something