How to parse date format?

空扰寡人 提交于 2019-12-04 02:40:51

问题


In one of the webservices my application is consuming, I encountered the following DateTime format.

"/Date(1395780377459)/"

Is this some standard date format? If so, how to parse this into DateTime object?

EDIT:

Thanks for the comments. So "/Date(1395780377459)/" corresponds to GMT: Tue, 25 Mar 2014 20:46:17 GMT . I was wondering how to parse this in .net. Tried:

         string test = "/Date(1395780377459)/";
         var datestring = test.Substring(6).TrimEnd(')','/');
         var date = new DateTime(long.Parse(datestring));

Tried this too:

 string test = "/Date(1395780377459)/";
 var datestring = test.Substring(6).TrimEnd(')','/');
 var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
 var result =  epoch.AddSeconds(long.Parse(datestring));

It doesn't work. Any idea what is going wrong?


回答1:


It looks like a unix timestamp:

http://www.epochconverter.com/

Tue, 25 Mar 2014 20:46:17 GMT

In .NET:

var epoch = new DateTime(1970,1,1,0,0,0,0,System.DateTimeKind.Utc);
var dt = epoch.AddMilliseconds(1395780377459);

(Source: How to convert a Unix timestamp to DateTime and vice versa?)




回答2:


I think the date is in milliseconds format. So you can try this:

DateTime date = new DateTime(long.Parse(1395780377459));
date.ToString("yyyy-MM-ddThh:mm:ssZ");

or simply

var time = TimeSpan.FromMilliseconds(1395780377459);

Also the if the date is in json format then you may try this as mentioned in this answer:

var date = new Date(parseInt(jsonDate.substr(6)));


来源:https://stackoverflow.com/questions/25112402/how-to-parse-date-format

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