# Formatting a verbose string to a datetime

**URL:** <https://forums.powershell.org/t/formatting-a-verbose-string-to-a-datetime/15474>\
**Category:** PowerShell Help\
**Created:** [January 12, 2021, 7:44am UTC](https://forums.powershell.org/t/formatting-a-verbose-string-to-a-datetime/15474 "2021-01-12T07:44:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dawsonweb](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/dawsonweb/32/22_2.png) [@dawsonweb](https://forums.powershell.org/u/dawsonweb)\
**Post date:** [January 12, 2021, 7:44am UTC](https://forums.powershell.org/t/formatting-a-verbose-string-to-a-datetime/15474/1 "2021-01-12T07:44:35Z")

</div>

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

&nbsp;

---

<div class="post-metadata">

**Author:** ![ralphmwr](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/ralphmwr/32/1064_2.png) [@ralphmwr](https://forums.powershell.org/u/ralphmwr)\
**Post date:** [January 12, 2021, 7:52am UTC](https://forums.powershell.org/t/formatting-a-verbose-string-to-a-datetime/15474/2 "2021-01-12T07:52:52Z")

</div>

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", "")
```

&nbsp;

---

<div class="post-metadata">

**Author:** ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)\
**Post date:** [January 12, 2021, 8:04am UTC](https://forums.powershell.org/t/formatting-a-verbose-string-to-a-datetime/15474/3 "2021-01-12T08:04:41Z")

</div>

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()
}
```

---

<div class="post-metadata">

**Author:** ![dawsonweb](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/dawsonweb/32/22_2.png) [@dawsonweb](https://forums.powershell.org/u/dawsonweb)\
**Post date:** [January 15, 2021, 9:34am UTC](https://forums.powershell.org/t/formatting-a-verbose-string-to-a-datetime/15474/4 "2021-01-15T09:34:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [January 15, 2021, 10:05am UTC](https://forums.powershell.org/t/formatting-a-verbose-string-to-a-datetime/15474/5 "2021-01-15T10:05:33Z")

</div>

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
```

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:28pm UTC](https://forums.powershell.org/t/formatting-a-verbose-string-to-a-datetime/15474/6 "2024-05-16T20:28:03Z")

</div>

Closed on May 16, 2024, 8:28pm UTC


