Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
DateTimeOffset.ToUnixTimeSeconds() Method in C#
The DateTimeOffset.ToUnixTimeSeconds() method in C# returns the number of seconds that have elapsed since the Unix epoch (1970-01-01T00:00:00Z). This method is particularly useful when working with Unix timestamps or when interfacing with systems that use Unix time.
Syntax
Following is the syntax −
public long ToUnixTimeSeconds();
Return Value
The method returns a long value representing the number of seconds since the Unix epoch. If the DateTimeOffset represents a time before the epoch, the return value will be negative.
Using ToUnixTimeSeconds with Different Time Zones
The Unix timestamp represents an absolute point in time, so changing the offset does not affect the result −
Example
using System;
public class Demo {
public static void Main() {
DateTimeOffset dateTimeOffset = new DateTimeOffset(2019, 11, 11, 6, 15, 45, new TimeSpan(3, 0, 0));
Console.WriteLine("DateTimeOffset = {0}", dateTimeOffset);
Console.WriteLine("Number of seconds: " + dateTimeOffset.ToUnixTimeSeconds());
DateTimeOffset res = dateTimeOffset.ToOffset(new TimeSpan(-5, 0, 0));
Console.WriteLine("\nDateTimeOffset (updated) = {0}", res);
Console.WriteLine("Number of seconds: " + res.ToUnixTimeSeconds());
}
}
The output of the above code is −
DateTimeOffset = 11/11/2019 6:15:45 AM +03:00 Number of seconds: 1573442145 DateTimeOffset (updated) = 11/10/2019 10:15:45 PM -05:00 Number of seconds: 1573442145
Using ToUnixTimeSeconds with Pre-Epoch Dates
When the DateTimeOffset represents a time before the Unix epoch, the method returns a negative value −
Example
using System;
public class Demo {
public static void Main() {
DateTimeOffset preEpoch = new DateTimeOffset(1967, 11, 11, 6, 15, 45, new TimeSpan(3, 0, 0));
Console.WriteLine("DateTimeOffset = {0}", preEpoch);
Console.WriteLine("Number of seconds: " + preEpoch.ToUnixTimeSeconds());
DateTimeOffset postEpoch = new DateTimeOffset(1975, 8, 15, 12, 30, 0, TimeSpan.Zero);
Console.WriteLine("\nDateTimeOffset = {0}", postEpoch);
Console.WriteLine("Number of seconds: " + postEpoch.ToUnixTimeSeconds());
}
}
The output of the above code is −
DateTimeOffset = 11/11/1967 6:15:45 AM +03:00 Number of seconds: -67553055 DateTimeOffset = 8/15/1975 12:30:00 PM +00:00 Number of seconds: 177418200
Using ToUnixTimeSeconds with Current Time
Example
using System;
public class Demo {
public static void Main() {
DateTimeOffset currentTime = DateTimeOffset.Now;
Console.WriteLine("Current DateTimeOffset = {0}", currentTime);
Console.WriteLine("Current Unix timestamp: " + currentTime.ToUnixTimeSeconds());
DateTimeOffset utcTime = DateTimeOffset.UtcNow;
Console.WriteLine("\nUTC DateTimeOffset = {0}", utcTime);
Console.WriteLine("UTC Unix timestamp: " + utcTime.ToUnixTimeSeconds());
}
}
The output of the above code is −
Current DateTimeOffset = 12/15/2023 3:45:30 PM -05:00 Current Unix timestamp: 1702675530 UTC DateTimeOffset = 12/15/2023 8:45:30 PM +00:00 UTC Unix timestamp: 1702675530
Conclusion
The DateTimeOffset.ToUnixTimeSeconds() method converts any DateTimeOffset to a Unix timestamp in seconds. The result is independent of time zone offset since Unix time represents an absolute point in time, making it useful for cross-platform time calculations and data exchange.
