Convert TimeZone to DateTime

These two small functions can be used to convert a specific Time Zone into a DateTime object. The returned DateTime object represents the current time in the specified Time Zone. This can be useful if you need to know what time time is right now in a different Time Zone.


function ConvertTo-DateTime {
<#
.SYNOPSIS
Converts a TimeZoneInfo object into a DateTime object for that time zone.
.DESCRIPTION
This function can be used to convert a specific time zone into a DateTime object
for that time zone.
.EXAMPLE
Get-TimeZone | Where-Object {$_.DisplayName -like '*Sydney*'} | ConvertTo-DateTime
.NOTES
Author: Øyvind Kallstad
Date: 03.05.2015
Version: 1.0
#>
[CmdletBinding()]
param (
[Parameter(ValueFromPipeline)]
[ValidateNotNullOrEmpty()]
[TimeZoneInfo] $TimeZone
)
$dateTime = ([datetime]::Now)
$dtOffset = $TimeZone.GetUtcOffset($dateTime)
Write-Output (($dateTime.Add($dtOffset)).ToUniversalTime())
}
function Get-TimeZone {
Write-Output ([System.TimeZoneInfo]::GetSystemTimeZones())
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s