Current Unix Timestamp SECONDS SINCE JAN 01 1970

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

How to convert Datetime to Unix timestamp in C#

🎈 🎈 🎈
+1

    using System;

    class Program
    {
        static void Main()
        {
            // DateTime (for example, 2007-10-19 19:30:09 UTC)
            DateTime dateTime = new DateTime(2007, 10, 19, 19, 30, 09, DateTimeKind.Utc);
    
            // Convert DateTime to Unix timestamp
            DateTimeOffset dateTimeOffset = new DateTimeOffset(dateTime);
            long unixTimestamp = dateTimeOffset.ToUnixTimeSeconds();
    
            Console.WriteLine("Unix Timestamp: " + unixTimestamp);
        }
    }            
        

Output:
Unix Timestamp: 1192822209
Example only. There may be multiple ways to perform this operation.

Code Explanation

To convert a DateTime object to a Unix timestamp in C#, you can use the DateTimeOffset class and its ToUnixTimeSeconds() method.
This method calculates the number of seconds that have passed since the Unix epoch (January 1, 1970, 00:00:00 UTC) for a given DateTime. First, a DateTime object is created representing the desired date and time, and then it is wrapped in a DateTimeOffset object.
By calling the ToUnixTimeSeconds() method, the C# program computes the equivalent Unix timestamp. For example, the DateTime value 2022-05-15 08:10:45 UTC is converted into the Unix timestamp 1652602245.



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