1

I have an API I call to get a certain DateTime, the response I get however is a string and I don't know how to make the string into a DateTime. The string I get back is this "1388064600", I know it should represent "26/12/13 - Thursday 2:30PM CEST".

How can I convert this string into a DateTime object? Is this some known format?

Dave Zych
  • 21,581
  • 7
  • 51
  • 66
Kevin Coenegrachts
  • 148
  • 1
  • 1
  • 7
  • `an API I call` OK, look at the documentation for the API if you aren't going to share the API. It will probably tell you EXACTLY what it is. – tnw Dec 27 '13 at 17:03
  • 1
    that looks like `ticks`. Pass those into a new DateTime as a long and it should create the proper date – ps2goat Dec 27 '13 at 17:03
  • 1
    Your number is a **timestamp**. You can find how to convert it here: http://stackoverflow.com/questions/249760/how-to-convert-unix-timestamp-to-datetime-and-vice-versa – MarcinJuraszek Dec 27 '13 at 17:03
  • Please do not blindly believe any of the responses here. The best person to answer about format is the guy who supports the API. If there is no mention in documentation nor there is a guy who supports it go and tell your authorities straight up. – danish Dec 27 '13 at 17:13

3 Answers3

3

I presume that you get Unix time, ("1388064600") that is defined as the number of seconds since midnight (UTC) on 1st January 1970. you can convert it with this function to date time :

public DateTime FromUnixTime(long unixTime)
{
    var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
    return epoch.AddSeconds(unixTime);
}
semirturgay
  • 4,151
  • 3
  • 30
  • 50
0

Check out this article, very helpful. Here is what they suggest.

System.DateTime dateTime = new System.DateTime(1970, 1, 1, 0, 0, 0, 0);
double timestamp = 1388064600;
// Add the number of seconds in UNIX timestamp to be converted.
dateTime = dateTime.AddSeconds(timestamp);
DROP TABLE users
  • 1,955
  • 14
  • 26
  • 1
    You left out a very important line from that article's code which defines the Unix epoch: `System.DateTime dateTime = new System.DateTime(1970, 1, 1, 0, 0, 0, 0);` – Lance U. Matthews Jan 13 '14 at 16:07
0

This is Unix Time See: How to convert a Unix timestamp to DateTime and vice versa?

These may help explain.

Community
  • 1
  • 1
Sam Greenhalgh
  • 5,952
  • 21
  • 37