Stalin Sebastian Stalin Sebastian - 3 months ago 9
Android Question

Convert datetime from xml datetime to epoch format

I'm feeding an android application with a xml having a datetime attribute.
Problem here is, the application is accepting datetime by 13 digit number like 1347712845061. I'm not able to find an options to do this type of conversion in c#.

Do anyone have any suggestion?

Answer

Assuming that sample value was meant to be Sat, 15 Sep 2012 12:40:45 UTC, it just means "number of milliseconds since the Unix epoch". (That's the information within a java.util.Date.) So you can write:

private static readonly DateTime UnixEpoch =
    new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

public static DateTime FromJavaDate(long millisSinceEpoch)
{
    return UnixEpoch.AddMilliseconds(millisSinceEpoch);
}

(You could use a DateTimeOffset too, which would always have an offset of 0.)

Comments