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.ToUnixTimeMilliseconds() Method in C#
The DateTimeOffset.ToUnixTimeMilliseconds() method in C# returns the number of milliseconds that have elapsed since the Unix epoch (1970-01-01T00:00:00.000Z). This method is useful for converting .NET datetime objects to Unix timestamps, which are commonly used in web APIs, databases, and cross-platform applications.
Syntax
Following is the syntax −
public long ToUnixTimeMilliseconds();
Return Value
The method returns a long value representing the number of milliseconds since January 1, 1970, 00:00:00 UTC. For dates before the Unix epoch, the return value will be negative.
Using ToUnixTimeMilliseconds() with Different TimeZones
This example demonstrates how the method works with different time zones. Note that the Unix timestamp remains the same regardless of the timezone offset, as it represents the same moment in UTC −
using System;
public class Demo {
public static void Main() {
DateTimeOffset dateTimeOffset = new DateTimeOffset(2019, 9, 10, 4, 20, 10, new TimeSpan(5, 0, 0));
Console.WriteLine("DateTimeOffset = {0}", dateTimeOffset);
Console.WriteLine("Number of milliseconds: " + dateTimeOffset.ToUnixTimeMilliseconds());
DateTimeOffset res = dateTimeOffset.ToOffset(new TimeSpan(-5, 0, 0));
Console.WriteLine("\nDateTimeOffset (updated) = {0}", res);
Console.WriteLine("Number of milliseconds: " + res.ToUnixTimeMilliseconds());
}
}
The output of the above code is −
DateTimeOffset = 9/10/2019 4:20:10 AM +05:00 Number of milliseconds: 1568071210000 DateTimeOffset (updated) = 9/9/2019 6:20:10 PM -05:00 Number of milliseconds: 1568071210000
Using ToUnixTimeMilliseconds() with Pre-Epoch Dates
When working with dates before January 1, 1970, the method returns negative values −
using System;
public class Demo {
public static void Main() {
DateTimeOffset dateTimeOffset = new DateTimeOffset(1967, 11, 11, 6, 15, 45, new TimeSpan(3, 0, 0));
Console.WriteLine("DateTimeOffset = {0}", dateTimeOffset);
Console.WriteLine("Number of milliseconds: " + dateTimeOffset.ToUnixTimeMilliseconds());
DateTimeOffset res = dateTimeOffset.ToOffset(new TimeSpan(-5, 0, 0));
Console.WriteLine("\nDateTimeOffset (updated) = {0}", res);
Console.WriteLine("Number of milliseconds: " + res.ToUnixTimeMilliseconds());
}
}
The output of the above code is −
DateTimeOffset = 11/11/1967 6:15:45 AM +03:00 Number of milliseconds: -67553055000 DateTimeOffset (updated) = 11/10/1967 10:15:45 PM -05:00 Number of milliseconds: -67553055000
Common Use Cases
-
API Integration − Converting .NET datetime objects to Unix timestamps for REST API calls.
-
Database Storage − Storing timestamps in a standardized format across different systems.
-
Cross-Platform Compatibility − Sharing timestamp data between different programming languages and platforms.
-
Performance − Unix timestamps are lightweight numeric values, efficient for calculations and comparisons.
Conclusion
The DateTimeOffset.ToUnixTimeMilliseconds() method provides an efficient way to convert .NET datetime objects to Unix timestamps in milliseconds. This method is timezone-aware and returns the same Unix timestamp regardless of the DateTimeOffset's timezone offset, making it ideal for cross-platform datetime handling.
