# format to datetime

**URL:** <https://forums.powershell.org/t/format-to-datetime/7891>\
**Category:** PowerShell Help\
**Created:** [January 20, 2017, 10:17am UTC](https://forums.powershell.org/t/format-to-datetime/7891 "2017-01-20T10:17:54Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![kubikula](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kubikula/32/20_2.png) [@kubikula](https://forums.powershell.org/u/kubikula)\
**Post date:** [January 20, 2017, 10:17am UTC](https://forums.powershell.org/t/format-to-datetime/7891/1 "2017-01-20T10:17:54Z")

</div>

Hello,

I have a external command generating an output.  
“Sep 12 23:59:59 2016 GMT”

I have been trying to parse it as a datetime format but I am stuck.  
Any help is appreciated.

Regards,  
Jacobo.

---

<div class="post-metadata">

**Author:** ![skalizar](https://avatars.discourse-cdn.com/v4/letter/s/ecc23a/32.png) [@skalizar](https://forums.powershell.org/u/skalizar)\
**Post date:** [January 20, 2017, 11:13am UTC](https://forums.powershell.org/t/format-to-datetime/7891/2 "2017-01-20T11:13:22Z")

</div>

Here is an ugly solution, maybe someone else has something better.

```
$m=[regex]::Matches("Sep 12 23:59:59 2016 GMT","(\w+ \d+) (\d+:\d+:\d+) (\d+) (\w+)")
get-date ($m[0].groups[1].value + ' ' + $m[0].groups[3].value + ' ' + $m[0].groups[2].value + ' ' + $m[0].groups[4].value)
```

This will come back converted to local time. If you don’t want that, leave off the time zone.

---

<div class="post-metadata">

**Author:** ![donj](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/donj/32/137_2.png) [@donj](https://forums.powershell.org/u/donj)\
**Post date:** [January 20, 2017, 11:40am UTC](https://forums.powershell.org/t/format-to-datetime/7891/3 "2017-01-20T11:40:13Z")

</div>

Huh. I’d have thought just [datetime]“Sep 12 23:59:59 2016 GMT” woulda done it. I’ll have to look and see if one of the System.DateTime constructors offers something more elegant.

---

<div class="post-metadata">

**Author:** ![richard-siddaway](https://avatars.discourse-cdn.com/v4/letter/r/d78d45/32.png) [@richard-siddaway](https://forums.powershell.org/u/richard-siddaway)\
**Post date:** [January 20, 2017, 2:48pm UTC](https://forums.powershell.org/t/format-to-datetime/7891/4 "2017-01-20T14:48:21Z")

</div>

The year is in the wrong place. You need

[datetime]“Sep 12 2016 23:59:59 GMT”

Also can only specify the time to be local or UTC/GMT

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [January 20, 2017, 3:05pm UTC](https://forums.powershell.org/t/format-to-datetime/7891/5 "2017-01-20T15:05:38Z")

</div>

The shortes I found:

```
‘Sep 12 23:59:59 2016 GMT’ -match ‘(\w+\s\d+\s+)((\d+:){2}\d+\s)(\d{4}\s\w{3})’
Get-Date $($Matches[2] + $Matches[1] + $Matches[4])
```

---

<div class="post-metadata">

**Author:** ![kubikula](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kubikula/32/20_2.png) [@kubikula](https://forums.powershell.org/u/kubikula)\
**Post date:** [January 21, 2017, 6:12pm UTC](https://forums.powershell.org/t/format-to-datetime/7891/6 "2017-01-21T18:12:09Z")

</div>

thanks everyone for your replies, and Olaf for the regex which works perfectly.

cheers,  
Jacobo

---

<div class="post-metadata">

**Author:** ![peter-jurgens-2](https://avatars.discourse-cdn.com/v4/letter/p/3bc359/32.png) [@peter-jurgens-2](https://forums.powershell.org/u/peter-jurgens-2)\
**Post date:** [January 21, 2017, 7:40pm UTC](https://forums.powershell.org/t/format-to-datetime/7891/7 "2017-01-21T19:40:34Z")

</div>

The ParseExact method of the system.datetime class is useful when working with an oddly formatted date time string:

[datetime]::ParseExact(“Sep 12 23:59:59 2016 GMT”,“MMM dd HH:mm:ss yyyy GMT”,[system.globalization.cultureinfo]::InvariantCulture)

I’m on mobile doing this from memory but I think I got everything right! If not let me know and I’ll confirm when in front of a PC.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [January 21, 2017, 7:50pm UTC](https://forums.powershell.org/t/format-to-datetime/7891/8 "2017-01-21T19:50:35Z")

</div>

> I'm on mobile doing this from memory but I think I got everything right!

You're right. Both thumbs up. ;-)

---

<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:39pm UTC](https://forums.powershell.org/t/format-to-datetime/7891/9 "2024-05-16T20:39:24Z")

</div>


