Convert string w/timezone to datetime type

This topic contains 0 replies, has 1 voice, and was last updated by Profile photo of Forums Archives Forums Archives 5 years, 5 months ago.

  • Author
    Posts
  • #5246

    by davidski at 2012-10-30 04:45:51

    I'm struggling with what I suspect is a simple datetime conversion. I have datestamps with the format: Oct 29, 2012 3:59:48 PM PDT. The PDT timezone is throwing me off. It sounds like timezone abbreviations of this sort are not supported under .NET. Is that correct? What's the simplest way of getting this converted over to a proper datetime object?

    Current failed approach is:
    $testdate = "Oct 29, 2012 3:59:48 PM PDT"
    $timeformat = "MMM d\, yyyy h\:m\:s tt zzz"
    [datetime]::ParseExact( $testdate, $timeinfo, $null)

    Many thanks for the help.

    by DonJ at 2012-10-30 07:44:11

    To my knowledge, they aren't supported. That means there's not going to be a simple way, unfortunately. This is something you might consider taking over to StackOverflow – I imagine some dev somewhere has had to solve this.

    by davidski at 2012-10-30 09:23:00

    Thanks, Don. Based on this http://stackoverflow.com/questions/2580356/parsing-rfc1123-formatted-dates-in-c-net-4-0 posting on StackOverflow I implemented the hack below. It's not robust, but it works for my limited needs.

    $testdate = "Oct 29, 2012 3:59:48 PM PDT" -replace "\s\w+$" ""
    $timeformat = "MMM d\, yyyy h\:m\:s tt"
    [datetime]::ParseExact( $testdate, $timeinfo, $null)

You must be logged in to reply to this topic.