Welcome › Forums › General PowerShell Q&A › Formatting a verbose string to a datetime
- This topic has 4 replies, 4 voices, and was last updated 1 week, 3 days ago by
Participant.
-
AuthorPosts
-
-
January 12, 2021 at 1:14 pm #285499
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
-
January 12, 2021 at 1:22 pm #285502
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).
PowerShell123$mydate = "Wed, 20 Jan, 2021 at 16:45 GMT +0000"Get-Date -Date ($mydate -replace "at","" -replace "\+0000", "") -
January 12, 2021 at 1:34 pm #285505
Get-Date can parse if you just remove the unnecessary stuff to attempt to get it to parse:
PowerShell123456789101112131415161718192021PS C:\Users\rasim> Get-Date '"Wed, 20 Jan, 2021 16:45 GMT +0000"'Get-Date : Cannot bind parameter 'Date'. Cannot convert value ""Wed, 20 Jan, 2021 16:45 GMT +0000"" to type "System.DateTime". Error: "String '"Wed, 20 Jan, 2021 16:45 GMT +0000"' was not recognized as a valid DateTime."At line:1 char:10+ Get-Date '"Wed, 20 Jan, 2021 16:45 GMT +0000"'+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+ CategoryInfo : InvalidArgument: (:) [Get-Date], ParameterBindingException+ FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.GetDateCommandPS C:\Users\rasim> Get-Date 'Wed, 20 Jan, 2021 16:45 GMT +0000'Get-Date : Cannot bind parameter 'Date'. Cannot convert value "Wed, 20 Jan, 2021 16:45 GMT +0000" to type "System.DateTime". Error: "String 'Wed, 20 Jan, 2021 16:45 GMT +0000' was not recognized as a valid DateTime."At line:1 char:10+ Get-Date 'Wed, 20 Jan, 2021 16:45 GMT +0000'+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+ CategoryInfo : InvalidArgument: (:) [Get-Date], ParameterBindingException+ FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.GetDateCommandPS C:\Users\rasim> Get-DateTuesday, January 12, 2021 1:19:12 PMPS C:\Users\rasim> Get-Date 'Wed, 20 Jan, 2021 16:45'Wednesday, January 20, 2021 4:45:00 PMAs it’s GMT 0000, that’s UTC so I just specified that in the formatted custom object:
PowerShell1234[pscustomobject]@{DateUTC = Get-Date -Date ("Wed, 20 Jan, 2021 16:45").Replace(' at','').Replace(' GMT +0000','')DateLocal = (Get-Date -Date ("Wed, 20 Jan, 2021 16:45").Replace(' at','').Replace(' GMT +0000','')).ToLocalTime()} -
January 15, 2021 at 3:04 pm #285970
Thank you very much. Still not sure how to make the timezone thing work but it’s a great start. Cheers.
-
January 15, 2021 at 3:35 pm #285973
Seems you can just remove the at and GMT.
PowerShell1234567$date = "Wed, 20 Jan, 2021 at 16:45 GMT +0000"$date = $date -replace 'at|GMT'get-date $dateWednesday, January 20, 2021 10:45:00 AMDifferent timezone
PowerShell1234567$date = "Wed, 20 Jan, 2021 at 16:45 GMT -0600"$date = $date -replace 'at|GMT'get-date $dateWednesday, January 20, 2021 4:45:00 PMAnd wrapped up in one line
PowerShell123Get-Date ("Wed, 20 Jan, 2021 at 16:45 GMT +0000" -replace 'at|GMT')Wednesday, January 20, 2021 10:45:00 AM
-
-
AuthorPosts
- You must be logged in to reply to this topic.