Sometimes you will find a “time” value in an application or database (normally on the back-end of an application) that is really a Unix Timestamp. Also known as “EPOCH,” the timestamp represents the number of seconds that have elapsed after midnight, 1970.
Below is a function to convert a Unix Timestamp to the local timezone of the computer running it; it also considers daylight savings time.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# Create function to receive UNIX Time Format and return it for the local timezone function Convert-UnixTime { Param( [Parameter(Mandatory=$true)][int32]$udate ) $Timezone = (Get-TimeZone) IF($Timezone.SupportsDaylightSavingTime -eq $True){ $TimeAdjust = ($Timezone.BaseUtcOffset.TotalSeconds + 3600) }ELSE{ $TimeAdjust = ($Timezone.BaseUtcOffset.TotalSeconds) } # Adjust time from UTC to local based on offset that was determined before. $udate = ($udate + $TimeAdjust) # Retrieve start of UNIX Format $orig = (Get-Date -Year 1970 -Month 1 -Day 1 -hour 0 -Minute 0 -Second 0 -Millisecond 0) # Return final time return $orig.AddSeconds($udate) } |