C# Get the Unix Timestamp

  1. C# Program to Get the Unix Timestamp Using DateTime.Now.Subtract().TotalSeconds Method
  2. C# Program to Get the Unix Timestamp Using DateTimeOffset.Now.ToUnixTimeSeconds() Method
  3. C# Program to Get the Unix Timestamp Using TimeSpan Struct Methods

A Unix timestamp is mainly used in Unix operating systems. But it is helpful for all operating systems because it represents the time of all time zones. Unix Timestamps represent the time in seconds. The Unix epoch started on 1st January 1970. So, Unix Timestamp is the number of seconds between a specific date and Unix epoch.

In C#, there are different methods to get a Unix timestamp. In this article, we are going to put our focus on these methods. Read till the end to discover these methods.

C# Program to Get the Unix Timestamp Using DateTime.Now.Subtract().TotalSeconds Method

The DateTime class is used to get the date and time. DateTime.Now gives the current date and time. The Subtract() method finds the difference in the current date and the Unix epoch. And the TotalSeconds property converts the resultant date and time to seconds.

The correct syntax to use this method is as follows:

DateTime.Now.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
//1970, 1, 1 is the Unix epoch

Example Code:

using System;

public class UnixTimestamp
{
    public static void Main()
    {
        
        Int32 unixTimestamp = (Int32)(DateTime.Now.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
        Console.WriteLine("The Unix Timestamp is {0}.", unixTimestamp);
    }
}

Output:

The Unix Timestamp is 1586782314.

C# Program to Get the Unix Timestamp Using DateTimeOffset.Now.ToUnixTimeSeconds() Method

The method DateTimeOffset.Now.ToUnixTimeSeconds() is a pre-defined method. It calculates the Unix timestamp in seconds. The Unix epoch value is already defined in the method, so, we don’t need to pass it as a parameter.

The correct syntax to use this method is as follows:

DateTimeOffset.Now.ToUnixTimeSeconds();

Example Code:

using System;

public class UnixTimestamp
{
    public static void Main()
    {
        var unixTimestamp = DateTimeOffset.Now.ToUnixTimeSeconds();
        Console.WriteLine("The Unix Timestamp is {0}.", unixTimestamp);
    }
}

Output:

The Unix Timestamp is 1586782273.

C# Program to Get the Unix Timestamp Using TimeSpan Struct Methods

In C#, TimeSpan is a structure that tells about the time interval. Its property Ticks is used to represent the time interval value. It can be used to find the Unix Timestamp.

The correct syntax to use this structure is as follows:

//Creating First Time Interval
TimeSpan epochTicks = new TimeSpan(new DateTime(1970, 1, 1).Ticks);
//Creating Second Time Interval and Substracting its Value from the First One
TimeSpan unixTicks = new TimeSpan(DateTime.Now.Ticks) - epochTicks;
//Converting time interval to seconds to represent Unix Timestamp
Int32 unixTimestamp = (Int32)unixTicks.TotalSeconds;

Example Code:

using System;

public class UnixTimestamp
{
    public static void Main()
    {    
          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);
    }
}

Output:

The Unix Timestamp is 1586782432.
comments powered by Disqus