Created
December 19, 2012 00:46
-
-
Save rdsimes/4333461 to your computer and use it in GitHub Desktop.
converts a PowerShell/.NET DateTime to a unix timestamp (Milliseconds since 1 Jan, 1970)
Useful for creating JSON that can be understood by the .NET JavaScriptSerializer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function ConvertTo-UnixTimestamp { | |
$epoch = Get-Date -Year 1970 -Month 1 -Day 1 -Hour 0 -Minute 0 -Second 0 | |
$input | % { | |
$milliSeconds = [math]::truncate($_.ToUniversalTime().Subtract($epoch).TotalMilliSeconds) | |
Write-Output $milliSeconds | |
} | |
} | |
Get-Date | ConvertTo-UnixTimestamp |
Ancient gist but as I stumbled to this from google, I will leave comment. Even in version 2 there is available Get-Date -UFormat %s
, which return unix timestamp with five decimals, so multiplying it will do the trick. Note: it will return System.String, not System.DateTime
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hopefully PS3 will make this redundant