HomeCSharpHow to get the Unix Timestamp in C# ?

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

This C# program calculates and displays an upper triangular matrix based on user input. Problem Statement: The program takes the...
This C# program serves as a demonstration of bitwise operators, which are fundamental operators used for manipulating individual bits in...
This C# program is designed to interchange or swap the columns of a matrix. A matrix is a two-dimensional array...