February 1st, 2017

PowerTip: Convert from UTC to my local time zone

Doctor Scripto
Scripter

Summary: Cloud and Datacenter Management MVP, Thomas Rayner, shows how write a function to convert from UTC to your local time zone.

Hey, Scripting Guy! Question I have a time that I’d like to convert from UTC to my local time zone. How can I do this?

Hey, Scripting Guy! Answer You can write your own function to do this, and use the [System.TimeZoneInfo] .NET class and associated methods to make this conversion easily. Here is an example:

function Convert-UTCtoLocal

{ param( [parameter(Mandatory=$true)] [String] $UTCTime )

$strCurrentTimeZone = (Get-WmiObject win32_timezone).StandardName $TZ = [System.TimeZoneInfo]::FindSystemTimeZoneById($strCurrentTimeZone) $LocalTime = [System.TimeZoneInfo]::ConvertTimeFromUtc($UTCTime, $TZ) }

The Doctor

Author

The "Scripting Guys" is a historical title passed from scripter to scripter. The current revision has morphed into our good friend Doctor Scripto who has been with us since the very beginning.

0 comments

Discussion are closed.