Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you convert epoch time in C#?

Tags:

c#

time

epoch

How do you convert Unix epoch time into real time in C#? (Epoch beginning 1/1/1970)

like image 928
hsatterwhite Avatar asked May 21 '10 15:05

hsatterwhite


People also ask

How do I convert epoch time to real time?

Convert from epoch to human-readable datemyString := DateTimeToStr(UnixToDateTime(Epoch)); Where Epoch is a signed integer. Replace 1526357743 with epoch. =(A1 / 86400) + 25569 Format the result cell for date/time, the result will be in GMT time (A1 is the cell with the epoch number).

How do I convert epoch time to manual date?

You can take an epoch time divided by 86400 (seconds in a day) floored and add 719163 (the days up to the year 1970) to pass to it. Awesome, this is as manual as it gets.

What is epoch time format?

In a computing context, an epoch is the date and time relative to which a computer's clock and timestamp values are determined. The epoch traditionally corresponds to 0 hours, 0 minutes, and 0 seconds (00:00:00) Coordinated Universal Time (UTC) on a specific date, which varies from system to system.

Is epoch time always 10 digits?

No. epoch time is how time is kept track of internally in UNIX. It's seconds, counting upward from January 1st, 1970.


1 Answers

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 property

DateTime 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); } 
like image 65
LukeH Avatar answered Oct 31 '22 04:10

LukeH