# Customizing Output Units of Dates.canonicalize

**URL:** https://discourse.julialang.org/t/customizing-output-units-of-dates-canonicalize/90569
**Category:** General Usage
**Tags:** question, dates, timeperiod
**Created:** [November 21, 2022, 4:40am UTC](https://discourse.julialang.org/t/customizing-output-units-of-dates-canonicalize/90569 "2022-11-21T04:40:52Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![notaprogrammer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/notaprogrammer/32/44511_2.png) [@notaprogrammer](https://discourse.julialang.org/u/notaprogrammer)
#### Post date: [November 21, 2022, 4:40am UTC](https://discourse.julialang.org/t/customizing-output-units-of-dates-canonicalize/90569/1 "2022-11-21T04:40:52Z")

</div>

Working on some exercises to learn programming and Julia, and I am attempting to calculate my little one’s age.

I see that Julia does almost all of the work compared to the solution I wrote yesterday in Perl where I had to do most of it manually, wanting to use only built-in functions (adding no modules).

What I would like to add to my Julia solution is an age output in “x months, y days” or some variation of “x months” and other smaller units.

Canonicalize already gives me “x weeks, y days” but I don’t see a way to tell Dates.canonicalize which units _I want_. It seems to decide for itself based on the documentation.

Note that my time duration is coming from Julia performing algebra on two dates.

_ **Is there any way to request specific units from Dates.canonicalize?** _

Thanks!

```julia
julia> birthday = Dates.Date(2022,9,7)
2022-09-07

julia> today_date = Dates.today()

julia> age = today_date - birthday
74 days

julia> Dates.canonicalize(age)
10 weeks, 4 days

```

```julia
help?> Dates.canonicalize
  canonicalize(::CompoundPeriod) -> CompoundPeriod

  Reduces the CompoundPeriod into its canonical form by applying the following rules:

    • Any Period large enough be partially representable by a coarser Period will be broken into multiple Periods (eg. Hour(30)
       becomes Day(1) + Hour(6))

```

---

<div class="post-metadata">

### Author: ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)
#### Post date: [November 21, 2022, 7:25am UTC](https://discourse.julialang.org/t/customizing-output-units-of-dates-canonicalize/90569/2 "2022-11-21T07:25:17Z")

</div>

`canonicalize` does what it does, there are no options.  
In your example, there is no good way to determine say, Months and Days from the total number of days because the number of days per month varies.

---

<div class="post-metadata">

### Author: ![chiraganand](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chiraganand/32/32787_2.png) [@chiraganand](https://discourse.julialang.org/u/chiraganand)
#### Post date: [November 21, 2022, 8:34am UTC](https://discourse.julialang.org/t/customizing-output-units-of-dates-canonicalize/90569/3 "2022-11-21T08:34:09Z")

</div>

Here is a small hackish solution for calculating number of months and days since the birthday assuming that one month is calculated from birth date till the same date in the next month:

```julia
function calculate_months(start_date::Date, end_date::Date)
    months = Month(0)
    date = start_date + Month(1)
    while(date <= end_date)
        date += Month(1)
        months += Month(1)
    end
    days = end_date - (start_date + months)
    print(months, ' ', days)
end

```

```julia
julia> calculate_months(birthday, today())
2 months 14 days

```

---

<div class="post-metadata">

### Author: ![notaprogrammer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/notaprogrammer/32/44511_2.png) [@notaprogrammer](https://discourse.julialang.org/u/notaprogrammer)
#### Post date: [November 21, 2022, 12:22pm UTC](https://discourse.julialang.org/t/customizing-output-units-of-dates-canonicalize/90569/4 "2022-11-21T12:22:54Z")

</div>

> [@JeffreySarnoff](#):
>
> `canonicalize` does what it does, there are no options.  
> In your example, there is no good way to determine say, Months and Days from the total number of days because the number of days per month varies.

Thanks, it certainly seemed that way, but since I just started out reading programming documentation in general I wanted to make sure I wasn’t missing something. 👍

---

<div class="post-metadata">

### Author: ![notaprogrammer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/notaprogrammer/32/44511_2.png) [@notaprogrammer](https://discourse.julialang.org/u/notaprogrammer)
#### Post date: [November 21, 2022, 12:30pm UTC](https://discourse.julialang.org/t/customizing-output-units-of-dates-canonicalize/90569/5 "2022-11-21T12:30:36Z")

</div>

Thank you for the code. It seems to do just what I was looking to do, and is brief thanks to the way the month type works. I will test it out! ⭐
