AM/PM in Date Format

Hi!

How do I get the am/pm information from a string to a DateTime?
I’ve got the following string:

str = "11/7/2018 1:22:51 PM"

and want to parse it as a DateTime:

DateTime(str, dateformat"m/d/yyyy H:mm:ss ?")

Which character should I use for the PM?

This is currently unsupported.

Also see this issue: 23870

2 Likes

Thanks!

Got it to work for my case with slight modifications

function parse_datetime_ampm(ds::String, fmt::DateFormat)::DateTime
    @show m = match(r"(\d{1,2}/\d{1,2}/\d{4}\s?\d{1,2}:\d{1,2}:\d{1,2})\s?(AM|PM)?"i, ds)
    dt = DateTime(m.captures[1], fmt) 
    ampm = m.captures[2]
    dt += (ampm != nothing && uppercase(ampm) == "PM" && Hour(dt).value != 12) ? Hour(12) : Hour(0)
end

Note that this has since been fixed in https://github.com/JuliaLang/julia/pull/32308, and will appear in julia 1.3