In previous article, I mentioned how to convert String to Float in C# (Various ways) but in this article, I have mentioned how we can convert epoc (unix) timestamp to datetime in C# and Datetime to unix (epoc) time with an example in C# console application.

Now, .NET Framework 4.6 and above and .NET Core has provided inbuilt support to transform the Dates to or from Unix Time (EPOC) represented by either seconds or milliseconds.

Unix to Datetime in C#

Unix time in seconds to UTC DateTimeOffset and then get datetime from it.

using System;

namespace EPOCDatetimeViceVersa
{
    class Program
    {
        static void Main(string[] args)
        {
            long epocSeconds = 1663591419; // seconds

            DateTimeOffset dateTimeOffset = DateTimeOffset.FromUnixTimeSeconds(epocSeconds);
            DateTime dateTime = dateTimeOffset.DateTime;
            Console.WriteLine(dateTime);
        }
    }
}

Output:

9/19/2022 12:43:39 PM

epoc-to-datetime-csharp

If you want to convert epoc milliseconds to datetime, then you can use below C# Code

DateTimeOffset dateTimeOffSet = DateTimeOffset.FromUnixTimeMilliseconds(1663591419000);
DateTime dateTime = dateTimeOffSet.DateTime;

Using Custom Method instead of inbuilt

If you want to use Custom method to convert Unix timestamp into datetime in C#, then you can use below method

public static DateTime UnixTimestampToDateTime(double unixTime)
{
    DateTime unixStart = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc);
    long unixTimeStampInTicks = (long) (unixTime * TimeSpan.TicksPerSecond);
    return new DateTime(unixStart.Ticks + unixTimeStampInTicks, System.DateTimeKind.Utc);
}

Above method, can help you in converting unix time with precision to datetime in C#.

Datetime to unix in C#

Now, if we want to convert current datetime into Unix (Epoc) time, then we can do it as below

using System;

namespace EPOCDatetimeViceVersa
{
    class Program
    {
        static void Main(string[] args)
        {
            long epocSeconds = new DateTimeOffset(DateTime.Now).ToUnixTimeSeconds(); // today's date into unix time in seconds.
            Console.WriteLine(epocSeconds);
        }
    }
}

Output:

1663592064

Using Custom Method instead of inbuilt

Again, if you want to use custom method instead of above unbuilt to convert datetime into unix timestamp, then here is it is:

using System;

namespace EPOCDatetimeViceVersa
{
    class Program
    {
        static void Main(string[] args)
        {          
            Console.WriteLine(DateTimeToUnixTimestamp(DateTime.Now));
        }
        public static double DateTimeToUnixTimestamp(DateTime dateTime)
        {
            DateTime unixStart = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc);
            long unixTimeStampInTicks = (dateTime.ToUniversalTime() - unixStart).Ticks;
            return (double)unixTimeStampInTicks / TimeSpan.TicksPerSecond;
        }
    }
}

Which will give output with precision unlike inbuilt methods.

1663592224.1456976

That's it, hope it helps.

You may also like to read:

Set Default Value to Property in C#

Select vs SelectMany in C# With Example

Format Code in Visual Studio (With Indentation)

Convert C# Class to JSON with Example

Converting String to Enum OR Enum to String in C#

Lambda expression in C# (With Examples)