1

#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?

Filed in: Columns, Tips and Tricks Tags: ,

One Response to "#PSTip Converting to the local time"

  1. orlando says:

    the shortest one, I´m a lazy typist 🙂 but the one including the Time reference (i.e. GMT) also appeals to me for its precision

Leave a Reply

Submit Comment

© 2017 PowerShell Magazine. All rights reserved. XHTML / CSS Valid.
Proudly designed by Theme Junkie.
%d bloggers like this: