snippetcsharpCritical
How do you convert epoch time in C#?
Viewed 0 times
youhowtimeconvertepoch
Problem
How do you convert Unix epoch time into real time in C#? (Epoch beginning 1/1/1970)
Solution
UPDATE 2024
In .NET core (>= 2.1)
UPDATE 2020
You can do this with DateTimeOffset
And if you need the
Original answer
I presume that you mean Unix time, which is defined as the number of seconds since midnight (UTC) on 1st January 1970.
In .NET core (>= 2.1)
DateTime.UnixEpoch.AddSeconds(epochSeconds)
DateTime.UnixEpoch.AddMilliseconds(epochMilliseconds)
UPDATE 2020
You can do this with DateTimeOffset
DateTimeOffset dateTimeOffset = DateTimeOffset.FromUnixTimeSeconds(epochSeconds);
DateTimeOffset dateTimeOffset2 = DateTimeOffset.FromUnixTimeMilliseconds(epochMilliseconds);
And if you need the
DateTime object instead of DateTimeOffset, then you can call the DateTime propertyDateTime dateTime = dateTimeOffset.DateTime;
Original answer
I presume that you mean Unix time, which is defined as the number of seconds since midnight (UTC) on 1st January 1970.
private static readonly DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
public static DateTime FromUnixTime(long unixTime)
{
return epoch.AddSeconds(unixTime);
}
Context
Stack Overflow Q#2883576, score: 777
Revisions (0)
No revisions yet.