How do I convert epoch time to date in Powershell?

System administrators run into issues with epoch time-to-date conversion. For this, you can use Powershell’s in-build cmdlets to convert epoch time to date format. Conversely, you can also convert the standard date format to epoch time format through the Powershell cmdlet.

Introduction

Epoch time is a representation of time in seconds and is a Unix standard. On more than one occasion, system administrators require the time to be changed between epoch to date and from date to epoch formats.

We will look at ways by which we can change epoch time to date format. For conversion from the current date format to epoch time values, we will share another study.

Before we convert epoch time to standard date format, it may be pertinent to mention that we set the epoch time to start from 1st January 1970. So, the value of time in seconds gets added to the standard date 01.01.1970 to find the standard date from an epoch time.

For our example, we will take an epoch time of 1666866800.930687318 seconds and convert it to a standard date format.

Using System.Timespan

Here are the steps that we will make use of to find the date from epoch time given in seconds:

  • We will assign the epoch time value to a variable
  • We will use the System.Timespan class to add seconds from the epoch time to the start date of 01.01.1970. The value so found is also stored in a variable.
  • We will display the converted date variable on the screen. You can choose to use this variable in any way you deem fit.

The following commands can be used to achieve conversion of epoch time to standard date format:

$epochtime1 = “1666866800.930687318”
$standardDate = (Get-Date -Date “01-01-1970”) + ([System.TimeSpan]::FromSeconds($epochtime1))
$standardDate

The command output of these commands is displayed hereunder in the screenshot.

Epoch time to date format using Powershell System Timespan.

For this conversion, we chose:

  • System.Timespan .NET class
  • We used the FromSeconds method to add the epoch time value in seconds to the base date of 01.01.1970.

Apart from the System.Timespan .NET class, we can also use the datetime class and ‘Add Seconds’ method to find the epoch time’s conversion to standard date format.

Using datetime class

We will again use the epoch time value of 1666866800.930687318 to find the standard date from this value.

$epochtime1 = “1666866800.930687318”
([datetime]’1/1/1970′).AddSeconds($epochtime1)

  • First, we have assigned the epoch time to a variable $epochtime1.
  • Then we used the DateTime class in Powershell to find the standard date format.

The output of this command is represented below in the screenshot.

Epoch time to standard format using datetime and AddSeconds method in Powershell.

You can choose to use the [DATETIME] class on a static value or a variable. I have used the variable as it is more comfortable and practical to assign the epoch time value to a variable in real-life practical scenarios.

Summary

In this Powershell tutorial, we have seen two ways by which we can change epoch time to standard date format. We have made use of the System.Timespan class and the Datetime class to convert epoch time values to the current standard date format.

Suggested Powershell Tutorials

The following Powershell tutorials help you in performing basic system administration tasks on a Windows computer.

How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Rajesh Dhawan

Rajesh Dhawan is a technology professional who loves to blog about smart wearables, Cloud computing and Microsoft technologies. He loves to break complex problems into manageable chunks of meaningful information.