-1

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?

softwarematter
  • 28,015
  • 64
  • 169
  • 263
  • 1
    You must be getting response in JSON format. This is JSON format for date – PM. Aug 04 '14 at 05:47
  • 1
    Refer `Roy Tinker's` answer from this http://stackoverflow.com/questions/206384/how-to-format-a-microsoft-json-date/2316066#2316066 question – Earth Aug 04 '14 at 05:51
  • 1
    @devnull: On SO, please always be more specific than just _"it doesn't work"_: Does that mean that the code does not compile? (If so, what compilation error do you get, and where?) Does is throw an exception at runtime? (If so, which one, and at which location?) Does it yield an unexpected result? etc. – stakx - no longer contributing Aug 04 '14 at 06:36
  • @devnull, Most probably, your webservice returns a json and json serializers can handle it very well(for ex Json.Net). How do you *comsume* your json? I don't think you have to do some extra work to parse it. – L.B Aug 04 '14 at 06:52

2 Answers2

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)));
Community
  • 1
  • 1
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
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?)

Community
  • 1
  • 1
Troels Larsen
  • 4,462
  • 2
  • 34
  • 54