1

I'm having a huge problem trying to figure out a json response from a REST endpoint I'm getting information from.

The date that is coming back looks like this 1319068800000

if I attempt to convert this value like so:

DateTime currentServerTime = DateTime.FromFileTimeUtc(1319068800000);

I get this output: 1/2/1601 12:38:26 PM ...obviously wrong :(

The REST endpoint I'm using has a cool HTML display of the json response, and their format of the same value looks like this: 2011/10/20 00:00:00 UTC

Can someone please shed some light on this for me? I'm trying to update the date through this REST API and when I send a date using this code

DateTime.Now.ToFileTimeUtc().ToString();

I get this result 129691518811163201, and when I send this to the API it bombs out saying I have an valid date. Thanks!

3 Answers 3

4

Both @Bill and @Christofer are right. You still need to convert the Unix time to a DateTime, though. Here's a small function that does this: Convert a Unix timestamp to a .NET DateTime

Code quoted from the site (by Simone Chiaretta):

static DateTime ConvertFromUnixTimestamp(double timestamp)
{
    DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
    return origin.AddSeconds(timestamp);
}


static double ConvertToUnixTimestamp(DateTime date)
{
    DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
    TimeSpan diff = date - origin;
    return Math.Floor(diff.TotalSeconds);
}
Sign up to request clarification or add additional context in comments.

1 Comment

+1 for the conversion functions, I usually use the exact same thing myself.
3

It looks like the number of milliseconds since Jan 1, 1970 12:00 am GMT. This is the internal timestamp format that is typically used in Java.

user=> (java.util.Date. 1319068800000)
Date Wed Oct 19 20:00:00 EDT 2011

2 Comments

I think it is commonly referred to as Unix time or POSIX time.
Number of seconds since Jan 1, 1970 is, but Java does milliseconds.
3

You can divide the returned number with 1000 before converting it to a DateTime. It seems to be returned as milliseconds since Unix epoch.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.