Add to Technorati Favorites
Welcome to ThePowerShellGuy.com Sign in | Join | Help

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\/\/

Published Friday, February 29, 2008 5:08 PM by MoW
Filed under: , ,

Comments

# Hey, PowerShell Guy, how do I get disk space starting with IP addresses?

I am a Powershell newbie.

I have three clusters of servers I need to monitor. I need to keep get a regular record of the Free disk Space on the D: and E: drives, and the growth rate of a subset of all directories.

Powershell is the new organisational standard, the Production environment will shortly be scaling up, and I need to work from IP addresses rather than names.

Tuesday, March 25, 2008 1:33 PM by Nostradormouse
Anonymous comments are disabled