问题
I see that JSON.NET has a DateTime converter:
string javascriptJson = JsonConvert.DeserializeObject(entry, new JavaScriptDateTimeConverter());
However I don't have a JSON object, I simply have a string:
/Date(1276146000000-0500)/
I could create an object, add the date, then parse it, but this seems common enough that there should be a way to do this in a single line. Is there anything out there?
回答1:
The quotes around the date string are required. Also, the returned value is a DateTime
, not a string
.
DateTime date =
JsonConvert.DeserializeObject<DateTime>("\"/Date(1276146000000-0500)/\"");
回答2:
Does this not work:
DateTime date = JsonConvert.DeserializeObject<DateTime>(
"/Date(1276146000000-0500)/", new JavaScriptDateTimeConverter());
回答3:
Here is a discussion about this: http://weblogs.asp.net/bleroy/archive/2008/01/18/dates-and-json.aspx
Just be sure to read all the comments which contain some good information.
来源:https://stackoverflow.com/questions/3310530/easiest-way-to-parse-a-javascript-date-in-c