Given the following JSON Date representation:
给出以下JSON日期表示:
"\/Date(1221644506800-0700)\/"
How do you deserialize this into it's JavaScript Date-type form?
如何将其反序列化为JavaScript数据类型的表单?
I've tried using MS AJAX JavaScrioptSerializer as shown below:
我试过使用MS AJAX JavaScrioptSerializer,如下所示:
Sys.Serialization.JavaScriptSerializer.deserialize("\/Date(1221644506800-0700)\/")
However, all I get back is the literal string date.
但是,我得到的只是字符串日期。
7 个解决方案
#1
28
Provided you know the string is definitely a date I prefer to do this :
如果你知道字符串一定是我喜欢的日期:
new Date(parseInt(value.replace("/Date(", "").replace(")/",""), 10))
#2
16
Bertrand LeRoy, who worked on ASP.NET Atlas/AJAX, described the design of the JavaScriptSerializer DateTime output and revealed the origin of the mysterious leading and trailing forward slashes. He made this recommendation:
伯特兰·勒罗伊,他在ASP工作。NET Atlas/AJAX,描述了JavaScriptSerializer DateTime输出的设计,并揭示了神秘的前斜线和后斜线的起源。他这个建议:
run a simple search for "\/Date((\d+))\/" and replace with "new Date($1)" before the eval (but after validation)
运行一个简单的搜索“\/Date(\d+) \/”,并在eval(验证后)之前用“new Date($1)”替换
I implemented that as:
我实现了这个:
var serializedDateTime = "\/Date(1271389496563)\/";
document.writeln("Serialized: " + serializedDateTime + "<br />");
var toDateRe = new RegExp("^/Date\\((\\d+)\\)/$");
function toDate(s) {
if (!s) {
return null;
}
var constructor = s.replace(toDateRe, "new Date($1)");
if (constructor == s) {
throw 'Invalid serialized DateTime value: "' + s + '"';
}
return eval(constructor);
}
document.writeln("Deserialized: " + toDate(serializedDateTime) + "<br />");
This is very close to the many of the other answers:
这与其他许多答案非常接近:
- Use an anchored RegEx as Sjoerd Visscher did -- don't forget the ^ and $.
- 使用一个固定的正则表达式是会维斯——别忘了^和$。
- Avoid string.replace, and the 'g' or 'i' options on your RegEx. "/Date(1271389496563)//Date(1271389496563)/" shouldn't work at all.
- 避免字符串。替换,以及你的正则表达式中的“g”或“i”选项。“/日期(1271389496563)/日期(1271389496563)/”根本不应该工作。
#3
10
A JSON value is a string, number, object, array, true, false or null. So this is just a string. There is no official way to represent dates in JSON. This syntax is from the asp.net ajax implementation. Others use the ISO 8601 format.
JSON值是一个字符串、数字、对象、数组、true、false或null。这只是一个字符串。JSON中没有表示日期的正式方式。此语法来自asp.net ajax实现。其他的使用ISO 8601格式。
You can parse it like this:
你可以这样来解析:
var s = "\/Date(1221644506800-0700)\/";
var m = s.match(/^\/Date\((\d+)([-+]\d\d)(\d\d)\)\/$/);
var date = null;
if (m)
date = new Date(1*m[1] + 3600000*m[2] + 60000*m[3]);
#4
5
The regular expression used in the ASP.net AJAX deserialize method looks for a string that looks like "/Date(1234)/" (The string itself actually needs to contain the quotes and slashes). To get such a string, you will need to escape the quote and back slash characters, so the javascript code to create the string looks like "\"\/Date(1234)\/\"".
net AJAX反序列化方法中使用的正则表达式查找一个看起来像“/Date(1234)/”的字符串(字符串本身实际上需要包含引号和斜杠)。要获得这样的字符串,您需要避免引号和反斜杠字符,因此创建字符串的javascript代码看起来像“\”\ \日期(1234)\/\“”。
This will work.
这将工作。
Sys.Serialization.JavaScriptSerializer.deserialize("\"\\/Date(1221644506800)\\/\"")
It's kind of weird, but I found I had to serialize a date, then serialize the string returned from that, then deserialize on the client side once.
这有点奇怪,但我发现我必须序列化一个日期,然后序列化从中返回的字符串,然后在客户端反序列化一次。
Something like this.
是这样的。
Script.Serialization.JavaScriptSerializer jss = new Script.Serialization.JavaScriptSerializer();
string script = string.Format("alert(Sys.Serialization.JavaScriptSerializer.deserialize({0}));", jss.Serialize(jss.Serialize(DateTime.Now)));
Page.ClientScript.RegisterStartupScript(this.GetType(), "ClientScript", script, true);
#5
3
For those who don't want to use Microsoft Ajax, simply add a prototype function to the string class.
对于那些不想使用Microsoft Ajax的人,只需向string类添加一个原型函数即可。
E.g.
如。
String.prototype.dateFromJSON = function () {
return eval(this.replace(/\/Date\((\d+)\)\//gi, "new Date($1)"));
};
Don't want to use eval? Try something simple like
不想使用eval?尝试一些简单的喜欢
var date = new Date(parseInt(jsonDate.substr(6)));
As a side note, I used to think Microsoft was misleading by using this format. However, the JSON specification is not very clear when it comes to defining a way to describe dates in JSON.
顺便说一句,我曾经认为微软使用这种格式是在误导别人。但是,在定义一种用JSON描述日期的方法时,JSON规范不是很清楚。
#6
1
The big number is the standard JS time
最大的数字是标准的JS时间
new Date(1221644506800)
Wed Sep 17 2008 19:41:46 GMT+1000 (EST)
星期三2008年9月17日19:41:46 GMT+1000(东部时间)
#7
1
Actually, momentjs supports this kind of format, you might do something like:
实际上,momentjs支持这种格式,你可以做以下事情:
var momentValue = moment(value);
momentValue.toDate();
This returns the value in a javascript date format
这将以javascript日期格式返回值。
#1
28
Provided you know the string is definitely a date I prefer to do this :
如果你知道字符串一定是我喜欢的日期:
new Date(parseInt(value.replace("/Date(", "").replace(")/",""), 10))
#2
16
Bertrand LeRoy, who worked on ASP.NET Atlas/AJAX, described the design of the JavaScriptSerializer DateTime output and revealed the origin of the mysterious leading and trailing forward slashes. He made this recommendation:
伯特兰·勒罗伊,他在ASP工作。NET Atlas/AJAX,描述了JavaScriptSerializer DateTime输出的设计,并揭示了神秘的前斜线和后斜线的起源。他这个建议:
run a simple search for "\/Date((\d+))\/" and replace with "new Date($1)" before the eval (but after validation)
运行一个简单的搜索“\/Date(\d+) \/”,并在eval(验证后)之前用“new Date($1)”替换
I implemented that as:
我实现了这个:
var serializedDateTime = "\/Date(1271389496563)\/";
document.writeln("Serialized: " + serializedDateTime + "<br />");
var toDateRe = new RegExp("^/Date\\((\\d+)\\)/$");
function toDate(s) {
if (!s) {
return null;
}
var constructor = s.replace(toDateRe, "new Date($1)");
if (constructor == s) {
throw 'Invalid serialized DateTime value: "' + s + '"';
}
return eval(constructor);
}
document.writeln("Deserialized: " + toDate(serializedDateTime) + "<br />");
This is very close to the many of the other answers:
这与其他许多答案非常接近:
- Use an anchored RegEx as Sjoerd Visscher did -- don't forget the ^ and $.
- 使用一个固定的正则表达式是会维斯——别忘了^和$。
- Avoid string.replace, and the 'g' or 'i' options on your RegEx. "/Date(1271389496563)//Date(1271389496563)/" shouldn't work at all.
- 避免字符串。替换,以及你的正则表达式中的“g”或“i”选项。“/日期(1271389496563)/日期(1271389496563)/”根本不应该工作。
#3
10
A JSON value is a string, number, object, array, true, false or null. So this is just a string. There is no official way to represent dates in JSON. This syntax is from the asp.net ajax implementation. Others use the ISO 8601 format.
JSON值是一个字符串、数字、对象、数组、true、false或null。这只是一个字符串。JSON中没有表示日期的正式方式。此语法来自asp.net ajax实现。其他的使用ISO 8601格式。
You can parse it like this:
你可以这样来解析:
var s = "\/Date(1221644506800-0700)\/";
var m = s.match(/^\/Date\((\d+)([-+]\d\d)(\d\d)\)\/$/);
var date = null;
if (m)
date = new Date(1*m[1] + 3600000*m[2] + 60000*m[3]);
#4
5
The regular expression used in the ASP.net AJAX deserialize method looks for a string that looks like "/Date(1234)/" (The string itself actually needs to contain the quotes and slashes). To get such a string, you will need to escape the quote and back slash characters, so the javascript code to create the string looks like "\"\/Date(1234)\/\"".
net AJAX反序列化方法中使用的正则表达式查找一个看起来像“/Date(1234)/”的字符串(字符串本身实际上需要包含引号和斜杠)。要获得这样的字符串,您需要避免引号和反斜杠字符,因此创建字符串的javascript代码看起来像“\”\ \日期(1234)\/\“”。
This will work.
这将工作。
Sys.Serialization.JavaScriptSerializer.deserialize("\"\\/Date(1221644506800)\\/\"")
It's kind of weird, but I found I had to serialize a date, then serialize the string returned from that, then deserialize on the client side once.
这有点奇怪,但我发现我必须序列化一个日期,然后序列化从中返回的字符串,然后在客户端反序列化一次。
Something like this.
是这样的。
Script.Serialization.JavaScriptSerializer jss = new Script.Serialization.JavaScriptSerializer();
string script = string.Format("alert(Sys.Serialization.JavaScriptSerializer.deserialize({0}));", jss.Serialize(jss.Serialize(DateTime.Now)));
Page.ClientScript.RegisterStartupScript(this.GetType(), "ClientScript", script, true);
#5
3
For those who don't want to use Microsoft Ajax, simply add a prototype function to the string class.
对于那些不想使用Microsoft Ajax的人,只需向string类添加一个原型函数即可。
E.g.
如。
String.prototype.dateFromJSON = function () {
return eval(this.replace(/\/Date\((\d+)\)\//gi, "new Date($1)"));
};
Don't want to use eval? Try something simple like
不想使用eval?尝试一些简单的喜欢
var date = new Date(parseInt(jsonDate.substr(6)));
As a side note, I used to think Microsoft was misleading by using this format. However, the JSON specification is not very clear when it comes to defining a way to describe dates in JSON.
顺便说一句,我曾经认为微软使用这种格式是在误导别人。但是,在定义一种用JSON描述日期的方法时,JSON规范不是很清楚。
#6
1
The big number is the standard JS time
最大的数字是标准的JS时间
new Date(1221644506800)
Wed Sep 17 2008 19:41:46 GMT+1000 (EST)
星期三2008年9月17日19:41:46 GMT+1000(东部时间)
#7
1
Actually, momentjs supports this kind of format, you might do something like:
实际上,momentjs支持这种格式,你可以做以下事情:
var momentValue = moment(value);
momentValue.toDate();
This returns the value in a javascript date format
这将以javascript日期格式返回值。