CodexBloom - Programming Q&A Platform

PowerShell 7.3 - implementing Converting DateTime to Different Time Zones Using [TimeZoneInfo]

๐Ÿ‘€ Views: 72 ๐Ÿ’ฌ Answers: 1 ๐Ÿ“… Created: 2025-06-16
PowerShell DateTime TimeZone

I need help solving I've been researching this but I'm working with a scenario while trying to convert a DateTime object to different time zones in PowerShell 7.3... I have a UTC DateTime that I want to convert to Eastern Standard Time (EST) and Pacific Standard Time (PST) using the `[TimeZoneInfo]` class. However, I'm getting unexpected results with the converted times.\n\nHereโ€™s the code Iโ€™m using:\n\n```powershell\ndateUtc = Get-Date -Date '2023-10-15 10:00:00' -Format 'yyyy-MM-dd HH:mm:ss' -AsUtc\n$estZone = [TimeZoneInfo]::FindTimeZoneById('Eastern Standard Time')\n$pstZone = [TimeZoneInfo]::FindTimeZoneById('Pacific Standard Time')\n\n$estTime = [TimeZoneInfo]::ConvertTime($dateUtc, $estZone)\n$pstTime = [TimeZoneInfo]::ConvertTime($dateUtc, $pstZone)\n\n"EST Time: $estTime"\n"PST Time: $pstTime"\n```\n\nWhen I run this, the output is as follows:\n\n````\nEST Time: 10/15/2023 10:00:00 AM\nPST Time: 10/15/2023 10:00:00 AM\n````\n\nIt seems that both conversions are producing the same result, which is not expected. I verified that the time zone IDs are correct, so Iโ€™m unsure why the conversion isnโ€™t behaving as anticipated. I also checked the local time settings on my machine, but everything looks fine there.\n\nI tried using the `[System.DateTime]::ToLocalTime()` method as an alternative, but it yielded similar results. Any ideas on what might be going wrong here or if there's a certain best practice I should follow when handling time zones in PowerShell? Thanks in advance! What's the correct way to implement this?