Unix timestamp to C# DateTime

less than 1 minute read

As an Unix timestamp is simply the number of seconds since 1 January 1970, it’s very simple to convert it to a C# DateTime object:

.NET < 4.6

int unixTimestamp = 1451606400; // 1 January 2016
DateTime dateTime = new DateTime(1970, 1, 1).AddSeconds(unixTimestamp);

.NET > 4.6

int unixTimestamp = 1451606400; // 1 January 2016
DateTime dateTime = DateTimeOffset.FromUnixTimeSeconds(unixTimestamp).DateTime;

Updated:

Leave a comment