# Can't parse AM/PM?

**URL:** <https://discourse.julialang.org/t/cant-parse-am-pm/55234>\
**Category:** General Usage\
**Tags:** dates\
**Created:** [February 13, 2021, 11:11pm UTC](https://discourse.julialang.org/t/cant-parse-am-pm/55234 "2021-02-13T23:11:38Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![pyrex41](https://avatars.discourse-cdn.com/v4/letter/p/4bbf92/32.png) [@pyrex41](https://discourse.julialang.org/u/pyrex41)\
**Post date:** [February 13, 2021, 11:11pm UTC](https://discourse.julialang.org/t/cant-parse-am-pm/55234/1 "2021-02-13T23:11:38Z")

</div>

I cannot seem to figure out how to parse a datetime string with am/pm:

```julia
using Dates

DateTime("2021-02-01 11:12AM EST", dateformat"y-m-d H:Mp xxx")

```

gives this error:

```julia
ERROR: ArgumentError: Unable to parse date time. Expected directive DatePart(p) at char 16
Stacktrace:
 [1] macro expansion at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.5/Dates/src/parse.jl:104 [inlined]
 [2] tryparsenext_core(::String, ::Int64, ::Int64, ::DateFormat{Symbol("y-m-d H:Mp xxx"),Tuple{Dates.DatePart{'y'},Dates.Delim{Char,1},Dates.DatePart{'m'},Dates.Delim{Char,1},Dates.DatePart{'d'},Dates.Delim{Char,1},Dates.DatePart{'H'},Dates.Delim{Char,1},Dates.DatePart{'M'},Dates.DatePart{'p'},Dates.Delim{String,4}}}, ::Bool) at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.5/Dates/src/parse.jl:38
 [3] macro expansion at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.5/Dates/src/parse.jl:150 [inlined]
 [4] tryparsenext_internal at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.5/Dates/src/parse.jl:125 [inlined]
 [5] parse(::Type{DateTime}, ::String, ::DateFormat{Symbol("y-m-d H:Mp xxx"),Tuple{Dates.DatePart{'y'},Dates.Delim{Char,1},Dates.DatePart{'m'},Dates.Delim{Char,1},Dates.DatePart{'d'},Dates.Delim{Char,1},Dates.DatePart{'H'},Dates.Delim{Char,1},Dates.DatePart{'M'},Dates.DatePart{'p'},Dates.Delim{String,4}}}) at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.5/Dates/src/parse.jl:282
 [6] DateTime(::String, ::DateFormat{Symbol("y-m-d H:Mp xxx"),Tuple{Dates.DatePart{'y'},Dates.Delim{Char,1},Dates.DatePart{'m'},Dates.Delim{Char,1},Dates.DatePart{'d'},Dates.Delim{Char,1},Dates.DatePart{'H'},Dates.Delim{Char,1},Dates.DatePart{'M'},Dates.DatePart{'p'},Dates.Delim{String,4}}}) at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.5/Dates/src/io.jl:482
 [7] top-level scope at REPL[4]:1

```

I tried using ZonedDateTime, replacing the “xxx” with “Z”, but I get the same error. Is this a bug, or am I missing something obvious?

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [February 13, 2021, 11:32pm UTC](https://discourse.julialang.org/t/cant-parse-am-pm/55234/2 "2021-02-13T23:32:04Z")

</div>

```julia
julia> DateTime("2021-02-01 11:12PM EST", dateformat"y-m-d I:MMp \E\S\T")
2021-02-01T23:12:00

```

seems to be a bug.

---

<div class="post-metadata">

**Author:** ![pixel27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pixel27/32/8902_2.png) [@pixel27](https://discourse.julialang.org/u/pixel27)\
**Post date:** [February 14, 2021, 1:24am UTC](https://discourse.julialang.org/t/cant-parse-am-pm/55234/3 "2021-02-14T01:24:39Z")

</div>

I’m not sure it’s possible:

[https://juliatime.github.io/TimeZones.jl/stable/conversions/](https://juliatime.github.io/TimeZones.jl/stable/conversions/)

> Note that with the exception of “UTC” and “GMT” time zone abbrevations cannot be parsed using the `Z` character code since most abbreviations are ambiguous. For example abbreviation “MST” could be interpreted as “Mountain Standard Time” (UTC-7) or “Moscow Summer Time” (UTC+3:31).

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [February 14, 2021, 11:31am UTC](https://discourse.julialang.org/t/cant-parse-am-pm/55234/4 "2021-02-14T11:31:34Z")

</div>

The time zone isn’t the bug I was refering to, sorry for being unclear, but I just ignored the time zone question of OP.  
The bug I mean is that the minute needs to be a double M, so that the p (=AM/PM) is interpreted correctly.

---

<div class="post-metadata">

**Author:** ![pixel27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pixel27/32/8902_2.png) [@pixel27](https://discourse.julialang.org/u/pixel27)\
**Post date:** [February 14, 2021, 1:00pm UTC](https://discourse.julialang.org/t/cant-parse-am-pm/55234/5 "2021-02-14T13:00:04Z")

</div>

Ahh, I see. I would be on the edge of if that is a bug or not. Without a space (or something) to separate the two fields the code would have to guess a little. For example these don’t work:

```julia
DateTime("20210201", dateformat"ymd")
DateTime("20210201", dateformat"ymmdd")

```

You have to do the full format for it to parse correctly:

```julia
DateTime("20210201", dateformat"yyyymmdd")

```

Granted with the minutes vs am/pm one is a numbers and the other letters so it could be inferred, but then that would make it special/different from the other identifiers. 🤷‍♂️

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [February 14, 2021, 2:34pm UTC](https://discourse.julialang.org/t/cant-parse-am-pm/55234/6 "2021-02-14T14:34:45Z")

</div>

Good points.  
I think AM/PM and some time zone information is quite common when parsing date/time strings. So it should be part of the documentation as examples. When there are examples it would be explicit how the format string should be given, and than it could be better classified as bug or edge case, despite it would be less important then 🙂  
The point is: parsing date/time is best documented as a large set of examples.
