Hey, PowerShell Guy, how to translate a Unix timestamp to a local time ?
This time not a translation from a Hey scriptingguy article, but a question that just came up in the #powerShell IRC channel,
how to convert a Unix timestamp to a local datetime ?
A Unix Timestamp is expressed in seconds since 1 January 1970 in UTC time, this function will convert the unix timestamp to a UTC time first, and then convert it to a Local time for the current timezone.
Function get-Unixdate ($UnixDate) {
[timezone]::CurrentTimeZone.ToLocalTime(([datetime]'1/1/1970').AddSeconds($UnixDate))
}
And now you can easy translate Unix timestamps like this :
PS C:\scripts> get-Unixdate 1204062874
Tuesday, February 26, 2008 10:54:34 PM
I did not make a -UTC parameter but if you want you can get back to UTC time like this ;-)
PS C:\scripts> (get-Unixdate 1204062874).ToUniversalTime()
Tuesday, February 26, 2008 9:54:34 PM
Enjoy,
Greetings /\/\o\/\/