Auto Ad Code

Friday, July 22, 2022

UNIX Time to DateTime - Timestamp to DateTime - C# (C Sharp)

Unix time is the number of seconds that have elapsed since the 01 January 1970 00:00:00 UTC. 

01 January 1970 00:00:00 UTC is called Unix epoch.

Following short and simple (2 liner) C# (CSharp) function will return the DateTime for the provided Unix Time (Timestamp)


public static DateTime UnixTimeToDateTime(long TimeStamp)
{
    if (TimeStamp.ToString().Length == 13)
    {

                //It means the timestamp is in milliseconds and we need seconds. 
                //So divide by 1000 to get seconds
                TimeStamp = TimeStamp / 1000;
    }

    DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, 0);
    return UnixEpoch.AddSeconds(TimeStamp);
}



unix time to datetime, unix time to date, timestamp to datetime, timestamp to date, c#, c sharp, csharp, asp.net, unix epoch, convert unix time to datetime c#, 13 digit, 3 extra dgits

No comments:

Post a Comment