How do I get a DateTime from a UNIX timestamp?

Often when consuming data from a web site, it will represent time with a UNIX timestamp.  This is the number of seconds since January 1, 1970.  It would be nice to turn this into a System.DateTime, but how?

DateTime FromUnixTime(Int64 unixTime)

{

DateTime time = new DateTime(unixTime * 10000000);

time = time.AddYears(1969);

return time;

}

The DateTime constructor takes the number of 100 nanosecond ticks since January 1, 0001, so multiplying by 10000000 translates seconds into 100 nanosecond intervals.  But the Unix era begins in 1970 and DateTime in 1, so to translate that we add 1969 years.

Comments

  • Anonymous
    May 13, 2007
    NathanA has a post showing you how to get a Date Time object from a Unix Time stamp (number of seconds

  • Anonymous
    July 16, 2007
    En algunas ocasiones tenemos que interactuar con sistemas que tiene este tipo de fecha: 1183875671, hombre