# How to decompose a DatePeriod or TimePeriod in tuple of (Y,m,d,H,M,S,s)?

**URL:** https://discourse.julialang.org/t/how-to-decompose-a-dateperiod-or-timeperiod-in-tuple-of-y-m-d-h-m-s-s/74929
**Category:** General Usage
**Tags:** dates
**Created:** [January 20, 2022, 11:00am UTC](https://discourse.julialang.org/t/how-to-decompose-a-dateperiod-or-timeperiod-in-tuple-of-y-m-d-h-m-s-s/74929 "2022-01-20T11:00:25Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)
#### Post date: [January 20, 2022, 11:00am UTC](https://discourse.julialang.org/t/how-to-decompose-a-dateperiod-or-timeperiod-in-tuple-of-y-m-d-h-m-s-s/74929/1 "2022-01-20T11:00:25Z")

</div>

```julia
using Dates
longPeriod = Date(2024,6,1) - Date(2022,1,20)

```

How can I decompose `longPeriod` (or a `TimePeriod`) in a tuple of (years,months,days,hours,minutes,seconds,milliseconds) ?

I know `canLongPeriod = Dates.canonicalize(longPeriod)` returns a CompoundPeriod object, but in terms of weeks and further, I don’t then know how to handle it.

But I guess the problem is complicated from the fact that 1 year or 1 month is not always the same number of days, so maybe my question doesn’t make much sense…

---

<div class="post-metadata">

### Author: ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)
#### Post date: [January 20, 2022, 11:04am UTC](https://discourse.julialang.org/t/how-to-decompose-a-dateperiod-or-timeperiod-in-tuple-of-y-m-d-h-m-s-s/74929/2 "2022-01-20T11:04:33Z")

</div>

Does a `Period` store information also on when it starts? If not, does a “TimedPeriod” with starts + length exists?

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [January 20, 2022, 2:17pm UTC](https://discourse.julialang.org/t/how-to-decompose-a-dateperiod-or-timeperiod-in-tuple-of-y-m-d-h-m-s-s/74929/4 "2022-01-20T14:17:31Z")

</div>

FWIW, by calling Python from Julia we can obtain the number of years, months, days, etc., elapsed between two dates:

```julia
using PyCall
datetime = pyimport("datetime")
dateutil = pyimport("dateutil")
relativedelta = pyimport("dateutil.relativedelta")
d1 = datetime.datetime(2022,1,20)
d2 = datetime.datetime(2024,6,1)
dt = dateutil.relativedelta.relativedelta(d2, d1)

# results:
dt.years, dt.months, dt.days, dt.hours, dt.minutes, dt.seconds, dt.microseconds
(2, 4, 12, 0, 0, 0, 0)

```

---

<div class="post-metadata">

### Author: ![Daneel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/daneel/32/175_2.png) [@Daneel](https://discourse.julialang.org/u/Daneel)
#### Post date: [January 21, 2022, 1:15pm UTC](https://discourse.julialang.org/t/how-to-decompose-a-dateperiod-or-timeperiod-in-tuple-of-y-m-d-h-m-s-s/74929/5 "2022-01-21T13:15:12Z")

</div>

```julia
julia> time2 = DateTime(2024,6,1)
2024-06-01T00:00:00

julia> time1 = DateTime(2022,1,20)
2022-01-20T00:00:00

julia> x = Dates.periods(Dates.canonicalize.(Dates.CompoundPeriod.(time2 - time1)))
2-element Vector{Period}:
 123 weeks
 2 days

```

It’s not exactly what you were looking for but it’s entirely in Julia. It appears that it the largest unit it returns is weeks. I don’t know exactly _why_ but my instant theory is that months and years aren’t fixed sizes absolutely.

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [January 21, 2022, 1:54pm UTC](https://discourse.julialang.org/t/how-to-decompose-a-dateperiod-or-timeperiod-in-tuple-of-y-m-d-h-m-s-s/74929/6 "2022-01-21T13:54:37Z")

</div>

@Daneel, according to OP:

> [@](#):
>
> I know ` Dates.canonicalize(longPeriod)` returns a CompoundPeriod object, but in terms of weeks and further, I don’t then know how to handle it.
