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.
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.
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))
The command output of these commands is displayed hereunder in the screenshot.
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”
- 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.
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.
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 to enable Remote Desktop using Powershell?
- Get CPU temperature using Powershell
- Get all hotfixes installed by date in Powershell
- How to find stopped services using Powershell?
- How to find running services in Powershell?
- How do I check if a directory exists in Powershell?
- How to get the first few lines of a file using Powershell?
- How to find the properties of an event log in Powershell?
- How to get event logs for an event using Powershell?
- How to export event logs to CSV file using Powershell?
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 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.