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
How to get the Unix timestamp in C#
A Unix timestamp is a system for describing time as the number of seconds that have elapsed since January 1, 1970, at 00:00:00 UTC. This epoch time is widely used across different operating systems and programming languages for consistent time representation.
Unix timestamps are particularly useful for storing dates in databases, comparing times across different time zones, and working with APIs that expect epoch time values.
What is Unix Timestamp?
The Unix timestamp represents time as a single integer value − the count of seconds since the Unix epoch (January 1, 1970, UTC). This makes it timezone-independent and easy to work with programmatically.
Using DateTime.Now.Subtract() Method
The traditional approach calculates the difference between the current time and the Unix epoch −
using System;
class Program {
static void Main(string[] args) {
Int32 unixTimestamp = (Int32)(DateTime.Now.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
Console.WriteLine("The Unix Timestamp is {0}", unixTimestamp);
}
}
The output of the above code is −
1596837896
Using DateTimeOffset.ToUnixTimeSeconds() Method
The modern and recommended approach uses the built-in ToUnixTimeSeconds() method −
using System;
class Program {
static void Main(string[] args) {
var unixTimestamp = DateTimeOffset.Now.ToUnixTimeSeconds();
Console.WriteLine("The Unix Timestamp is {0}", unixTimestamp);
}
}
The output of the above code is −
1596819230
Using TimeSpan Struct Methods
An alternative approach using TimeSpan for calculating the difference in ticks −
using System;
class Program {
static void Main(string[] args) {
TimeSpan epochTicks = new TimeSpan(new DateTime(1970, 1, 1).Ticks);
TimeSpan unixTicks = new TimeSpan(DateTime.Now.Ticks) - epochTicks;
Int32 unixTimestamp = (Int32)unixTicks.TotalSeconds;
Console.WriteLine("The Unix Timestamp is {0}", unixTimestamp);
}
}
The output of the above code is −
1596839083
Converting Unix Timestamp Back to DateTime
You can also convert a Unix timestamp back to a readable date format −
using System;
class Program {
static void Main(string[] args) {
long unixTimestamp = 1596819230;
// Convert to DateTime
DateTime dateTime = DateTimeOffset.FromUnixTimeSeconds(unixTimestamp).DateTime;
Console.WriteLine("Unix timestamp {0} converts to: {1}", unixTimestamp, dateTime);
}
}
The output of the above code is −
Unix timestamp 1596819230 converts to: 8/7/2020 2:40:30 PM
Comparison of Methods
| Method | Advantages | Disadvantages |
|---|---|---|
| DateTime.Subtract() | Works in older .NET versions | Manual calculation, more verbose |
| DateTimeOffset.ToUnixTimeSeconds() | Built-in method, cleaner code | Requires .NET Framework 4.6 or later |
| TimeSpan approach | Precise tick-based calculation | Most complex implementation |
Conclusion
Getting Unix timestamps in C# can be accomplished through multiple methods. The DateTimeOffset.ToUnixTimeSeconds() method is the most modern and recommended approach, while DateTime.Subtract() provides compatibility with older .NET versions. Choose the method that best fits your project requirements and .NET version.
