#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 C:\> [DateTime]::Parse('Mon, 07 Jul 2014 18:00:22 +0000')
Monday, July 07, 2014 11:30:22 PM

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

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

PS C:\> [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 C:\> [System.TimeZone]::CurrentTimeZone.ToLocalTime('Mon, 07 Jul 2014 18:00:22 +0000')
Monday, July 07, 2014 11:30:22 PM

PS C:\> [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?

About the author: Ravikanth C

Ravikanth is the founder and editor of the PowerShell Magazine. He is also a PowerShell MVP who works at Dell Inc. He blogs at http://www.ravichaganti.com/blog and you can follow him on Twitter @ravikanth.

Related Posts