Easiest way to parse a Javascript date in C#?

送分小仙女□ 提交于 2020-02-04 05:13:12

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!