#PSTip Converting to the local time

Several RSS feeds that I refer regularly have a publication date set to another time zone. For my own tracking purpose, I needed a way to convert it to the local time zone and store the date. There are several ways to do this but the following are my favorite ways of achieving this and in that order.

Using DateTime

The Parse method of DateTime .NET class can be used in PowerShell to convert between different time zones.

PS> [DateTime]::Parse('Mon, 07 Jul 2014 18:00:22 +0000')
Monday, July 07, 2014 11:30:22 PM

PS> [DateTime]::Parse('Thu, 21 Nov 2013 22:40:12 GMT')
Friday, November 22, 2013 4:10:12 AM

PS> [DateTime]"Mon, 07 Jul 2014 18:00:22 +0000"
Monday, July 07, 2014 11:30:22 PM

PS> [DateTime]"Thu, 21 Nov 2013 22:40:12 GMT"
Friday, November 22, 2013 4:10:12 AM

Using TimeZone.CurrentTimeZone

We can also use the ToLocalTime method of the TimeZone .NET class:

PS> [System.TimeZone]::CurrentTimeZone.ToLocalTime('Mon, 07 Jul 2014 18:00:22 +0000')
Monday, July 07, 2014 11:30:22 PM

PS> [System.TimeZone]::CurrentTimeZone.ToLocalTime('Thu, 21 Nov 2013 22:40:12 GMT')
Friday, November 22, 2013 4:10:12 AM

What is your favorite way of doing this?

Share on: