How to get the Unix Timestamp in C# ?

You can get the Unix timestamp in C# by subtracting the epoch time of 1st January 1970 from current UTC time.

How to get the Unix Timestamp in C# ?

Here’s a sample code snippet demonstrating how to get the unix timestamp in .NET.

using System;
public class Hello {
    public static void Main() {
        int unixTimestamp = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)))
        .TotalSeconds;
        System.Console.WriteLine(unixTimestamp);
    }
}

Output

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...