# ParseExact with odd date/time format as input fails

**URL:** <https://forums.powershell.org/t/parseexact-with-odd-date-time-format-as-input-fails/21658>\
**Category:** PowerShell Help\
**Created:** [February 15, 2023, 4:03pm UTC](https://forums.powershell.org/t/parseexact-with-odd-date-time-format-as-input-fails/21658 "2023-02-15T16:03:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![PinPinPoola](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/pinpinpoola/32/1779_2.png) [@PinPinPoola](https://forums.powershell.org/u/PinPinPoola)\
**Post date:** [February 15, 2023, 4:03pm UTC](https://forums.powershell.org/t/parseexact-with-odd-date-time-format-as-input-fails/21658/1 "2023-02-15T16:03:55Z")

</div>

Hi,

I am trying to convert a odd date/time string with the format “MMM dd, HH:mm zzz” into a [datetime] datatype, so I can convert it to my time zone; and I am struggling with ParseExtract.

The string (example “Feb 14, 14:37 PST”) is _helpfully_ provided from the data feed without the year; but it is the current year.

I have tried pre-pending the current year into the input string; but that hasn’t worked.

This is where I am at code wise:

```auto
$InputString = "Feb 14, 14:37 PST"
$CurrentYear = Get-Date -UFormat %Y
$InputStringWithYear = $CurrentYear + " " + $InputString  
$OutputDateTime = [datetime]::ParseExact($InputStringWithYear, "yyyy MMM dd, HH:mm zzz", $null)

```

and this is the error:

```auto
Exception calling "ParseExact" with "3" argument(s): "String was not recognized as a valid DateTime."
At line:4 char:1
+ $OutputDateTime = [datetime]::ParseExact($InputStringWithYear, "yyyy ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : FormatException

```

Would appreciate it if anyone can help or give me an alternative approach to try.

I thought this would be straightforward 🤣

Cheers  
Pin.

---

<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:** [February 15, 2023, 5:11pm UTC](https://forums.powershell.org/t/parseexact-with-odd-date-time-format-as-input-fails/21658/2 "2023-02-15T17:11:05Z")

</div>

According to this stackoverflow thread

> <https://stackoverflow.com/questions/241789/parse-datetime-with-time-zone-of-form-pst-cest-utc-etc>

you will have to replace the time zone abbreviation with its offset to get the correct result.

So if your time zone is allways `PST` and if that stands for `Pacific Standard Time` you could do something like this:

```auto
$InputString = 'Feb 14, 14:37 PST'
$InputStringWithOffset = $InputString -replace 'PST','-8'
[datetime]::ParseExact($InputStringWithOffset, 'MMM dd, HH:mm z',$null)

```

And you actually don’t need to add a year as the current year seems to be the default.

---

<div class="post-metadata">

**Author:** ![PinPinPoola](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/pinpinpoola/32/1779_2.png) [@PinPinPoola](https://forums.powershell.org/u/PinPinPoola)\
**Post date:** [February 15, 2023, 5:21pm UTC](https://forums.powershell.org/t/parseexact-with-odd-date-time-format-as-input-fails/21658/3 "2023-02-15T17:21:04Z")

</div>

Thank you @Olaf

My _faulty_ hypothesis was that the missing year was the issue, I had not even considered the PST time zone reference might have been the problem. 🫢

Your help is very much appreciated.

Cheers,  
Pin

---

<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:** [February 15, 2023, 5:28pm UTC](https://forums.powershell.org/t/parseexact-with-odd-date-time-format-as-input-fails/21658/4 "2023-02-15T17:28:33Z")

</div>

Well … sometimes we can’t see the forest for the trees. 😉🤷🏽‍♂️

---

<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:15pm UTC](https://forums.powershell.org/t/parseexact-with-odd-date-time-format-as-input-fails/21658/5 "2024-05-16T20:15:11Z")

</div>

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


