PowerShell Convert Epoch Time to DateTime

PowerShell convert epoch time to DateTime

Before moving towards various solutions to convert the Epoch time to Datetime, it is essential to know the Epoch time. Epoch time, POSIX time or Unix time is the system for denoting a point in time to a single number. This single number is the number of seconds (not counting leap seconds) that have elapsed since January 1, 1970, 00:00:00 UTC (Coordinated Universal Time).

Why is Unix Epoch time necessary? It is commonly used in programming languages and computer systems to denote and manipulate times and dates. It simplifies the Time and Date calculations by representing a single number indicating a point in time; it also lets us compare date and time with various systems and time zones.

Another question you may have is whether the number would be an integer or float representing the Epoch time. It can be either of them. The Epoch time can be a float point number or an integer based on the system and programming language. Let’s continue with various solutions to convert Epoch time to DateTime.

Using the AddSeconds() Method of DateTime Class

Use the AddSeconds() method of the DateTime class to convert the Epoch time to DateTime in PowerShell.

First, we defined the $epochTime variable and initialized it with the 1615395869 value, an Epoch time value. Next, we used the New-Object cmdlet to create a DateTime object with 1970,1,1,0,0,0 parameters, which denoted the starting point of Epoch time (also called Unix Epoch). Finally, with the DateTime object, we chained the AddSeconds() method, which took $epochTime as a parameter.

This method was used to add the value of $epochTime to the DateTime object to get a new DateTime object showing the human readable date and time corresponding to the specified Epoch time value. Finally, we used the Write-Output cmdlet to print the value of the $dateTimeObject variable on the PowerShell console.

Don’t panic if you get different output because the output for the above code depends on your system’s time zone settings.

An alternative script of the above is given below.

Here, we used the -Type parameter to mention the type of object that we wanted to create and specify the list of arguments using the -ArgumentList parameter.

Using the Get-Date Cmdlet with the AddSeconds() Method

Use the Get-Date cmdlet with the AddSeconds() method to convert the Epoch time to DateTime in PowerShell.

This example is similar to the example presented in the previous section. Here, we used the Get-Date cmdlet to get the current date and time and added $epochTime to it using the AddSeconds() method to get a DateTime object, which we stored in the $dateTimeObject variable. Further, we used the Write-Output cmdlet to display its value on the PowerShell console.

In the above example, the starting point of the Epoch time was the current date based on your system and time zone.

Using the Get-Date Cmdlet with the System.TimeSpan library

We can also use the Get-Date cmdlet with System.TimeSpan, a Microsoft .NET class library, to convert the specified Epoch time to human readable date and time. See the following example.

Here, we used Get-Date with the -Date parameter, which was set to 01-01-1970 to create a DateTime object. Further, we added the number of seconds represented by $epochTime to it using the TimeSpan.FromSeconds() method. So, what is the starting point of Epoch time here? Yes, you got it right! the starting point of Epoch time is 01-01-1970.

Until this point, the Epoch Timestamp was taken from the past; what if we want to work with the current Epoch Timestamp? In that case, we can use the following solution.

Convert Current Epoch Time to DateTime

To convert the actual (current) Epoch time to human-readable DateTime:

  • Use the Get-Date cmdlet with the -UFormat parameter to get the current Epoch Time.
  • Use the System.DateTimeOffset class to convert the above Epoch time to regular DateTime.

In the first line of the above code, we used Get-Date with the -UFormat parameter whose value was set to %s; this command returned the current time as the number of seconds elapsed since January 1, 1970, 00:00:00 UTC, which is a Unix Epoch Time. Next, we assigned this Epoch time to the $epochTime variable.

Next, we used the FromUnixTimeSeconds() method of the System.DateTimeOffset class to transform the retrieved Epoch time to a DateTimeOffset object, representing a starting point in time with an offset from UTC.

After that, we accessed the DateTime property of the DateTimeOffset object to get a corresponding DateTime object, denoting the same starting point in time but without the offset. Finally, this starting point in time was stored in the $dateTimeObject variable, which we used with the Write-Output cmdlet to print its value on the PowerShell console.

We can make the above output more readable by chaining the ToString() method with $dateTimeObject; see the following example. The ToString() method does nothing but transforms to the String type.

That’s all about PowerShell convert Epoch time to DateTime.

Was this post helpful?

Leave a Reply

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