Borbin the 🐱

Unix time in PowerShell

05 February, 2013


The Unix time is the number of seconds since 1 January 1970.
In PowerShell:

((Get-Date "02/05/2013") - (Get-Date "01/01/1970")).TotalSeconds

1360022400

To get back the date from the number of seconds, use:

(Get-Date "01/01/1970").AddSeconds(1360022400)

2/5/2013 12:00:00 AM



If your system has a different locale, use the culture specific writing. For example using German ('de-DE'):

((Get-Date "05.02.2013") - (Get-Date "01.01.1970")).TotalSeconds

1360022400

(Get-Date "01.01.1970").AddSeconds(1360022400)

Dienstag, 5. Februar 2013 00:00:00



In case you have a format in a locale that is different from your PowerShell locale:

$DateTime = [DateTime]::Parse("05/02/2013", [System.Globalization.CultureInfo]'fr-FR')
($DateTime - (Get-Date "01.01.1970")).TotalSeconds

Note the difference between french and american date order: 05/02/2013 - 02/05/2013