Create datetime object of other timezone in Powershell Create datetime object of other timezone in Powershell powershell powershell

Create datetime object of other timezone in Powershell


This solution worked well for me:

$cstzone = [System.TimeZoneInfo]::FindSystemTimeZoneById("Central Standard Time")$csttime = [System.TimeZoneInfo]::ConvertTimeFromUtc((Get-Date).ToUniversalTime(), $cstzone)

You can then manipulate the $csttime variable just like a datetime object:

Get-Date $csttime.AddHours(-1) -f "MM\dd\yyyy HH:mm:ss"

References: http://msdn.microsoft.com/en-us/library/system.timezoneinfo.converttimefromutc(v=vs.110).aspxhttp://technet.microsoft.com/en-us/library/ee692801.aspx


Working with TimeZones can get pretty tricky. Thank god for the updates they added in .NET 3.5 and 4.0. I worked on several time zone projects and in 2.0 is was nearly impossible without iterating the registry and building your own list of TimeZones.

Now you can get a list of TimeZones by using the TimeZoneInfo class (which you seem to be familiar with):http://msdn.microsoft.com/en-us/library/system.timezoneinfo.aspx

Just beware that a system that may not be updating automatically from Windows Update could potentially have different time zones than a system that is totally up to date. Microsoft pushes out updates for time zones when needed.

I think your code seems OK. To be honest, I'm not much of a PowerShell dev (C# for me), but it seems good.