Current Unix Timestamp SECONDS SINCE JAN 01 1970

Current UTC Time
2025-08-20
Convert Unix Timestamp to Datetime in C#

How to convert Unix timestamp to Datetime in C#

🎈 🎈 🎈
+1

    using System;

    class Program
    {
        static void Main()
        {
            // Unix timestamp (Eg, 1233604286, the number of seconds since the Unix epoch)
            long unixTimestamp = 1233604286;
    
            // Convert Unix timestamp to DateTime
            DateTimeOffset dateTimeOffset = DateTimeOffset.FromUnixTimeSeconds(unixTimestamp);
            DateTime dateTime = dateTimeOffset.UtcDateTime;
    
            Console.WriteLine("Converted Date (UTC): " + dateTime);
        }
    }
        

Output:
Converted Date (UTC): 2009-02-02 19:51:26
Example only. There may be multiple ways to perform this operation.

Code Explanation

In C#, you can convert a Unix timestamp (the number of seconds since January 1, 1970) to a DateTime object using the DateTimeOffset.FromUnixTimeSeconds() method.
This method takes the Unix timestamp as input and converts it into a DateTimeOffset object, which represents the specific moment in time. Since Unix timestamps are typically in UTC, the UtcDateTime property of the DateTimeOffset object is used to retrieve the corresponding date and time in UTC.
This allows you to transform a Unix timestamp into a human-readable DateTime object. For example, the Unix timestamp '1233604286' is converted into 2009-02-02 19:51:26 UTC.



Other useful C# date functions

Function/Method Description Example
DateTime.Now Returns the current date and time based on the local system's time zone. DateTime.Now returns 2024-08-28 12:34:56
DateTime.UtcNow Returns the current date and time in UTC. DateTime.UtcNow returns 2024-08-28 16:34:56
DateTime.Today Returns the current date with the time component set to 00:00:00. DateTime.Today returns 2024-08-28 00:00:00
DateTime.Parse() Converts a string representation of a date and time into a DateTime object. DateTime.Parse("2024-08-28 12:34:56") returns 2024-08-28 12:34:56
DateTime.TryParse() Tries to parse a string into a DateTime object, returning a boolean value to indicate success. DateTime.TryParse("2024-08-28", out date) returns true and 2024-08-28 in date
DateTime.AddDays() Adds a specified number of days to a DateTime object. DateTime.Now.AddDays(5) returns 2024-09-02 12:34:56
DateTime.AddMonths() Adds a specified number of months to a DateTime object. DateTime.Now.AddMonths(2) returns 2024-10-28 12:34:56
DateTime.AddYears() Adds a specified number of years to a DateTime object. DateTime.Now.AddYears(1) returns 2025-08-28 12:34:56
DateTime.Subtract() Subtracts a DateTime or TimeSpan from a DateTime object. DateTime.Now.Subtract(TimeSpan.FromDays(5)) returns 2024-08-23 12:34:56
DateTime.ToString() Converts a DateTime object into a string using a specified format. DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") returns 2024-08-28 12:34:56
DateTime.ToUniversalTime() Converts a local DateTime object to UTC time. DateTime.Now.ToUniversalTime() returns 2024-08-28 16:34:56
TimeSpan Represents a time interval and is used for date/time arithmetic. TimeSpan.FromDays(5) returns 5.00:00:00 (5 days)
DateTimeOffset.UtcNow Gets the current date and time, along with the time zone's offset from UTC. DateTimeOffset.UtcNow returns 2024-08-28T16:34:56+00:00
Current Unix Timestamp SECONDS SINCE JAN 01 1970

Current UTC Time
2025-08-20
Ad Banner Placeholder
Ad Banner Placeholder
Ad Banner Placeholder
Ad Banner Placeholder