Times in Unix are stored as a number of seconds after the 1st January 1970 00:00:00.
Therefore to convert a Unix Epoch time to a .net DateTime value, we need to instantiate a DateTime of the Unix Epoch and add the number of seconds.
public DateTime UnixEpochTimeToDateTime(double epochTimeStamp)
{
return new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc).AddSeconds(epochTimeStamp).ToLocalTime();
}
public double DateTimeToUnixEpochTime(DateTime dateTime)
{
TimeSpan timeDifference = dateTime - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
return Math.Floor(timeDifference.TotalSeconds);
}
This article was published on the 18th April 2008