Tuesday, June 29, 2010

Get Unix Time/ POSIX Time in c#

Recently needed to convert the time into Unix format in C#. Since the Unix time format is basically a count of the seconds passed since 1/1/1970 (Unix epoch), this can be easily done using the DateTime object.


int unixTime = (int) (DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds;


If you are trying to convert some other DateTime value to POSIX time, make sure that it is first converted to UTC.

No comments:

Post a Comment