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
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
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).
$mydate = "Wed, 20 Jan, 2021 at 16:45 GMT +0000" Get-Date -Date ($mydate -replace "at","" -replace "\+0000", "")
Get-Date can parse if you just remove the unnecessary stuff to attempt to get it to parse:
PS 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.GetDateCommand PS 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.GetDateCommand PS C:\Users\rasim> Get-Date Tuesday, January 12, 2021 1:19:12 PM PS C:\Users\rasim> Get-Date 'Wed, 20 Jan, 2021 16:45' Wednesday, January 20, 2021 4:45:00 PM
As it’s GMT 0000, that’s UTC so I just specified that in the formatted custom object:
[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() }
Thank you very much. Still not sure how to make the timezone thing work but it’s a great start. Cheers.
Seems you can just remove the at and GMT.
$date = "Wed, 20 Jan, 2021 at 16:45 GMT +0000" $date = $date -replace 'at|GMT' get-date $date Wednesday, January 20, 2021 10:45:00 AM
Different timezone
$date = "Wed, 20 Jan, 2021 at 16:45 GMT -0600" $date = $date -replace 'at|GMT' get-date $date Wednesday, January 20, 2021 4:45:00 PM
And wrapped up in one line
Get-Date ("Wed, 20 Jan, 2021 at 16:45 GMT +0000" -replace 'at|GMT') Wednesday, January 20, 2021 10:45:00 AM