# Parsing corporate DateTime format with calendar week

**URL:** <https://discourse.julialang.org/t/parsing-corporate-datetime-format-with-calendar-week/124725>\
**Category:** General Usage\
**Tags:** question, parsing, datetime\
**Created:** [January 13, 2025, 1:22pm UTC](https://discourse.julialang.org/t/parsing-corporate-datetime-format-with-calendar-week/124725 "2025-01-13T13:22:09Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![MatthijsCox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/matthijscox/32/42831_2.png) [@MatthijsCox](https://discourse.julialang.org/u/MatthijsCox)\
**Post date:** [January 14, 2025, 1:09pm UTC](https://discourse.julialang.org/t/parsing-corporate-datetime-format-with-calendar-week/124725/4 "2025-01-14T13:09:58Z")

</div>

I’m worried the solution is wrong. Let me explain.

`2025-01-01` started on a Wednesday, so it should be called `2501.3` right? That means we need to offset the final date with 3 days, not 1.

We can get this value with `dayofweek(Date(Year(2025))`, which returns 3.

(You can check your implementation is false, `dayofweek(myparse("2505.1 13:30"))` returns 3 instead of 1)

So I think the solution is:

```julia
using Dates
function weekdate_to_datetime(str::AbstractString)
    m = match(r"(?<year>\d\d)(?<week>\d\d)\.(?<day>\d)\s(?<hour>\d\d):(?<minute>\d\d)", str)
    if isnothing(m) || length(m.captures) != 5
        error("incorrect weekday format, expected \"yymm.dd hh:mm\"")
    end
    year = 2000 + parse(Int, m[:year])
    week = parse(Int, m[:week])
    day = parse(Int, m[:day])
    hour = parse(Int, m[:hour])
    minute = parse(Int, m[:minute])

    day_offset = dayofweek(DateTime(year))
    return (DateTime(year) + Week(week - 1) + Day(day) - Day(day_offset) +
            Hour(hour) + Minute(minute))
end

```

Note the following funny result due to the week date definition we use here:

```julia
julia> weekdate_to_datetime("2453.1 13:30")
2024-12-30T13:30:00

julia> weekdate_to_datetime("2501.1 13:30")
2024-12-30T13:30:00

```

Maybe you want to throw an error if someone inputs a week date of a year that starts in the previous year like `"2501.1"` ?

Also note that there may be funny ISO definitions as hinted at in this discussion: [Determine date range given year and week number - General Usage - Julia Programming Language](https://discourse.julialang.org/t/determine-date-range-given-year-and-week-number/93878/7)

---

_[View the full topic](https://discourse.julialang.org/t/parsing-corporate-datetime-format-with-calendar-week/124725)._
