• Javascript
  • Python
  • Go

Converting DateTime to UNIX time in PowerShell

When working with dates and times in PowerShell, you may come across the need to convert a DateTime object to UNIX time. UNIX time, also kno...

When working with dates and times in PowerShell, you may come across the need to convert a DateTime object to UNIX time. UNIX time, also known as Epoch time, is a system for representing dates and times as a single number. This number represents the number of seconds that have elapsed since January 1, 1970, at 00:00:00 UTC. This format is commonly used in computer systems, and being able to convert between DateTime and UNIX time can be useful in various scenarios.

To convert a DateTime object to UNIX time in PowerShell, we first need to understand the structure of a DateTime object. In PowerShell, a DateTime object is a .NET object that contains properties such as Year, Month, Day, Hour, Minute, Second, and Millisecond. These properties allow us to access and manipulate the individual components of a date and time.

To start, let's create a DateTime object using the Get-Date cmdlet in PowerShell:

$date = Get-Date

This will create a DateTime object with the current date and time. Now, to convert this object to UNIX time, we can use the ToUniversalTime() method and then the ToFileTimeUtc() method. This will give us the number of 100-nanosecond intervals that have elapsed since January 1, 1601, at 00:00:00 UTC. We can then divide this number by 10,000 to get the number of seconds, which is equivalent to UNIX time.

Here is an example of how to convert a DateTime object to UNIX time in PowerShell:

$date = Get-Date

$unixTime = $date.ToUniversalTime().ToFileTimeUtc() / 10000000

Now, let's say we want to convert a specific DateTime object to UNIX time, such as December 25, 2020, at 12:00 PM. We can create a DateTime object with these specific values and then use the same method as before to convert it to UNIX time.

$specificDate = Get-Date -Year 2020 -Month 12 -Day 25 -Hour 12 -Minute 0 -Second 0

$unixTime = $specificDate.ToUniversalTime().ToFileTimeUtc() / 10000000

We can also convert UNIX time back to a DateTime object using the DateTime.FromFileTimeUtc() method. This method takes in the number of 100-nanosecond intervals and returns a DateTime object with the corresponding date and time.

Here is an example of how to convert UNIX time to a DateTime object in PowerShell:

$unixTime = 1608886800

$date = [DateTime]::FromFileTimeUtc($unixTime * 10000000)

In this example, we are using the .NET Framework's DateTime class to call the FromFileTimeUtc() method. We multiply the UNIX time by 10,000 to get the number of 100-nanosecond intervals before passing it as a parameter.

In addition to converting between DateTime and UNIX time, PowerShell also allows us to format the output of a DateTime object in various ways. For example, we can use the ToString() method to specify a custom format string, which will display the date and time in a specific format.

Here are some examples of custom format strings in PowerShell:

$date.ToString("MM/dd/yyyy") #output: 04/15/2021

$date.ToString("dddd, MMMM dd, yyyy") #output: Thursday, April 15, 2021

$date.ToString("hh:mm tt") #output: 10:30 AM

For a complete list of custom format strings, you can refer to Microsoft's documentation on DateTime formats.

In conclusion, being able to convert DateTime objects to UNIX time and vice versa can be a useful skill to have in your PowerShell arsenal. It allows you to work with dates and times in a different format, which may be required in certain situations. Additionally, knowing how to format the output of a DateTime object can help you display the date and time in a way that is more suitable for your needs. So next time you come across the need to convert DateTime to UNIX time in PowerShell, you'll know just what to do.

Related Articles