How to convert to a Date from Timestamp

The two below JSON valus (created, udated) are some type of timestamp. The provider (Edmunds API) didn't specify anything besides "timestamp". I have tried standard UNIX conversion to .Net Datetime but nothing is working.

"created":1380292641721,"updated":1380312585558

The following two methods didn't work:

 internal DateTime ConvertUnixEpochTime(long epoch)
    {
        long baseTicks = 621355968000000000;
        long tickResolution = 10000000;

        //long epoch = 1225815911;

        long epochTicks = (epoch * tickResolution) + baseTicks;
        return new DateTime(epochTicks, DateTimeKind.Utc);
    }

    public DateTime UnixTimeStampToDateTime(long unixTimeStamp)
    {
        // Unix timestamp is seconds past epoch
        System.DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0);
        dtDateTime = dtDateTime.AddSeconds(unixTimeStamp).ToLocalTime();
        return dtDateTime;
    }

Any suggestions?

Jon Skeet
people
quotationmark

Your current code assumes the value is in seconds - whereas it looks like it's in milliseconds since the Unix epoch. So you want:

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

...

DateTime dateTime = UnixEpoch.AddMilliseconds(value);

Or using Noda Time:

Instant instant = Instant.FromMillisecondsSinceUnixEpoch(value);

people

See more on this question at Stackoverflow