Formatting a verbose string to a datetime

Welcome Forums General PowerShell Q&A Formatting a verbose string to a datetime

Viewing 4 reply threads
  • Author
    Posts
    • #285499
      Participant
      Topics: 5
      Replies: 3
      Points: 67
      Rank: Member

      Hi,

      I’ve got a verbose string in this format, i’d like to convert to a datetime() taking account of the zone.

      format: "Wed, 20 Jan, 2021 at 16:45 GMT +0000"

      Any ideas?

      Thanks all.

      MickyD

       

    • #285502
      Participant
      Topics: 5
      Replies: 265
      Points: 1,053
      Helping Hand
      Rank: Community Hero

      Playing around with the string, yielded that if the +0000 and “at” are removed the string can be converted to [datetime] object.  If all the values you want to convert are GMT +0000 then it’s pretty simple string manipulation.  If there are variations i.e. GMT +0600 then it would be a little more complicated but doable.  Here is a simple string manipulation (assuming everything is GMT +0000).

       

    • #285505
      Participant
      Topics: 17
      Replies: 1957
      Points: 4,015
      Helping Hand
      Rank: Community Hero

      Get-Date can parse if you just remove the unnecessary stuff to attempt to get it to parse:

      As it’s GMT 0000, that’s UTC so I just specified that in the formatted custom object:

    • #285970
      Participant
      Topics: 5
      Replies: 3
      Points: 67
      Rank: Member

      Thank you very much. Still not sure how to make the timezone thing work but it’s a great start. Cheers.

    • #285973
      Participant
      Topics: 9
      Replies: 707
      Points: 2,848
      Helping Hand
      Rank: Community Hero

      Seems you can just remove the at and GMT.

      Different timezone

      And wrapped up in one line

Viewing 4 reply threads
  • You must be logged in to reply to this topic.