How to Convert DateTime to Unix Timestamp in C#
By FoxLearn 1/13/2025 9:32:26 AM 890
To convert a DateTime
to a timestamp (i.e., Unix timestamp), the process depends on whether you want the result in seconds or milliseconds.
How to convert DateTime to Unix Timestamp in C#?
If you want to convert a DateTime
object to a Unix timestamp (seconds since January 1, 1970), you can do the following:
// c# get utc timestamp DateTime dateTime = DateTime.UtcNow; // Convert DateTime to UNIX timestamp (seconds) long unixTimestamp = (long)(dateTime - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds; // c# datetime to unix timestamp (seconds)
Output: convert datetime to timestamp c#
- Current UTC time: 1/10/2025 6:56:16 AM
- UNIX timestamp: 1736492176
This calculates the number of seconds elapsed since the Unix epoch (1970-01-01T00:00:00Z
) until the specified DateTime
object.
You can also use DateTimeOffset to get unix timestamp.
// Convert DateTime to DateTimeOffset for easier UNIX timestamp conversion DateTimeOffset dto = new DateTimeOffset(DateTime.UtcNow); // Get UNIX timestamp in seconds string unixTimeInSeconds = dto.ToUnixTimeSeconds().ToString(); // c# datetime to unix epoch (seconds) // Get UNIX timestamp in milliseconds string unixTimeInMilliseconds = dto.ToUnixTimeMilliseconds().ToString(); // c# datetime to unix epoch (milliseconds)
Output: example c# datetime to timestamp
- Current UTC time: 1/10/2025 6:58:23 AM +00:00
- UNIX timestamp: 1736492303
- UNIX timestamp, including milliseconds: 1736492303500
You can create an extension method in C# to convert a DateTime
to a timestamp.
Convert DateTime to Unix Timestamp C#
// c# .net datetime to timestamp public static class DateTimeExtensions { // Converts a DateTime to UNIX time (seconds) public static string ToUnixTimeInSeconds(this DateTime dateTime) { DateTimeOffset dto = new DateTimeOffset(dateTime.ToUniversalTime()); return dto.ToUnixTimeSeconds().ToString(); } // Converts a DateTime to UNIX time (milliseconds) public static string ToUnixTimeInMilliseconds(this DateTime dateTime) { DateTimeOffset dto = new DateTimeOffset(dateTime.ToUniversalTime()); return dto.ToUnixTimeMilliseconds().ToString(); } }
To use the DateTimeExtensions
methods, you simply need to call the extension methods on a DateTime
object.
using System; class Program { static void Main() { // Example DateTime (current date and time) DateTime currentDateTime = DateTime.Now; // Convert current DateTime to UNIX time (seconds) string unixTimeSeconds = currentDateTime.ToUnixTimeInSeconds(); Console.WriteLine("UNIX Time (Seconds): " + unixTimeSeconds); // Convert current DateTime to UNIX time (milliseconds) string unixTimeMilliseconds = currentDateTime.ToUnixTimeInMilliseconds(); Console.WriteLine("UNIX Time (Milliseconds): " + unixTimeMilliseconds); } }
Output:
UNIX Time (Seconds): 1684416570 UNIX Time (Milliseconds): 1684416570456
In this example:
currentDateTime.ToUnixTimeInSeconds()
converts theDateTime
to UNIX time in seconds.currentDateTime.ToUnixTimeInMilliseconds()
converts theDateTime
to UNIX time in milliseconds.
Ensure that your DateTimeExtensions
class is in the same project or namespace as your main code, or add a using
statement to the correct namespace where it's defined.