# Formating Dates.CompoundPeriod

**URL:** <https://discourse.julialang.org/t/formating-dates-compoundperiod/59167>\
**Category:** Data\
**Tags:** dates, time\
**Created:** [April 13, 2021, 8:23am UTC](https://discourse.julialang.org/t/formating-dates-compoundperiod/59167 "2021-04-13T08:23:03Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Laco\_Kovac](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laco_kovac/32/20684_2.png) [@Laco\_Kovac](https://discourse.julialang.org/u/Laco_Kovac)\
**Post date:** [April 13, 2021, 8:23am UTC](https://discourse.julialang.org/t/formating-dates-compoundperiod/59167/1 "2021-04-13T08:23:03Z")

</div>

I have variable of type Dates.CompoundPeriod  
I need to change it to String in format HH:MM:SS (value will never exceed 1 hour)

```julia
julia> x = Dates.CompoundPeriod(Dates.Hour(12), Dates.Minute(13))
12 hours, 13 minutes

julia> Dates.format(x, dateformat"HH:MM:SS")
ERROR: MethodError: no method matching format(::Dates.CompoundPeriod, ::DateFormat{Symbol("HH:MM:SS"),Tuple{Dates.DatePart{'H'},Dates.Delim{Char,1},Dates.DatePart{'M'},Dates.Delim{Char,1},Dates.DatePart{'S'}}})
Closest candidates are:
  format(::TimeType, ::DateFormat) at julia\stdlib\v1.5\Dates\src\io.jl:535
  format(::TimeType, ::DateFormat, ::Any) at julia\stdlib\v1.5\Dates\src\io.jl:535
  format(::Any, ::Dates.DatePart{'s'}, ::Any) at julia\stdlib\v1.5\Dates\src\io.jl:203

```

Is there any standard way to do this? Or do I have to parse string as  
n1 hours, n2 minutes, n3 seconds  
?

Thank you.

---

<div class="post-metadata">

**Author:** ![quinnj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/quinnj/32/11_2.png) [@quinnj](https://discourse.julialang.org/u/quinnj)\
**Post date:** [April 13, 2021, 2:35pm UTC](https://discourse.julialang.org/t/formating-dates-compoundperiod/59167/2 "2021-04-13T14:35:51Z")

</div>

You should parse/convert your CompoundPeriod into a `Dates.Time` type object. It has support for formatting.

---

<div class="post-metadata">

**Author:** ![shubh\_b](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shubh_b/32/20697_2.png) [@shubh\_b](https://discourse.julialang.org/u/shubh_b)\
**Post date:** [May 11, 2021, 7:00am UTC](https://discourse.julialang.org/t/formating-dates-compoundperiod/59167/3 "2021-05-11T07:00:13Z")

</div>

So far, in Julia converting a `CompoundPeriod` type object to any other `Period` type or `TimeType` is not straightforward. @Laco_Kovac, you can notice another topic [here](https://discourse.julialang.org/t/how-can-i-convert-a-compoundperiod-type-object-to-period-type-in-julia/60930).

@quinnj, I think you are trying to say something like this:

```julia
julia> using Dates

julia> x = Dates.CompoundPeriod(Dates.Hour(12), Dates.Minute(13))
12 hours, 13 minutes

julia> Dates.format(Dates.Time(x.periods[1], x.periods[2]), dateformat"HH:MM:SS")
"12:13:00"

```

---

<div class="post-metadata">

**Author:** ![Laco\_Kovac](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laco_kovac/32/20684_2.png) [@Laco\_Kovac](https://discourse.julialang.org/u/Laco_Kovac)\
**Post date:** [May 12, 2021, 7:56am UTC](https://discourse.julialang.org/t/formating-dates-compoundperiod/59167/4 "2021-05-12T07:56:15Z")

</div>

Thank you!

---

<div class="post-metadata">

**Author:** ![Laco\_Kovac](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laco_kovac/32/20684_2.png) [@Laco\_Kovac](https://discourse.julialang.org/u/Laco_Kovac)\
**Post date:** [May 12, 2021, 8:16am UTC](https://discourse.julialang.org/t/formating-dates-compoundperiod/59167/5 "2021-05-12T08:16:15Z")

</div>

Dates.format(Dates.Time(x.periods…), dateformat"HH:MM:SS")

That is the best solution for my issue, because x could contain any of the 3 (hours, minutes, seconds).

---

<div class="post-metadata">

**Author:** ![anon91151494](https://avatars.discourse-cdn.com/v4/letter/a/838e76/32.png) [@anon91151494](https://discourse.julialang.org/u/anon91151494)\
**Post date:** [October 23, 2024, 11:43am UTC](https://discourse.julialang.org/t/formating-dates-compoundperiod/59167/6 "2024-10-23T11:43:12Z")

</div>

> [@shubh\_b](#):
>
> ```julia
> julia> using Dates
> 
> julia> x = Dates.CompoundPeriod(Dates.Hour(12), Dates.Minute(13))
> 12 hours, 13 minutes
> 
> julia> Dates.format(Dates.Time(x.periods[1], x.periods[2]), dateformat"HH:MM:SS")
> "12:13:00"
> 
> ```

> [@Laco\_Kovac](#):
>
> Dates.format(Dates.Time(x.periods…), dateformat"HH:MM:SS")

What I don’t understand with either solution is will this work in general?

What happens if you have a `CompoundPeriod` which is `Days(1) + Seconds(1)`.

Is this represented as a `Tuple`?

- What is `compound_period[1]` ? `Days(1)`?
- What is `compound_period[2]` ? `Seconds(1)`?

Then following on to the second quote -

- `Dates.Time(compound_period...)` \< would this behave correctly?

---

<div class="post-metadata">

**Author:** ![anon91151494](https://avatars.discourse-cdn.com/v4/letter/a/838e76/32.png) [@anon91151494](https://discourse.julialang.org/u/anon91151494)\
**Post date:** [October 23, 2024, 11:54am UTC](https://discourse.julialang.org/t/formating-dates-compoundperiod/59167/7 "2024-10-23T11:54:54Z")

</div>

I’ve managed to answer some of my questions using the REPL.

Here are my findings.

Using the splay syntax appears to work in general. I guess Julia does something intelligent to match up keyword arguments. (I’m not super familiar with Julia yet.)

Here’s an example of something which works in general.

```julia
z = Dates.CompoundPeriod(Dates.Hour(12), Dates.Second(30), Dates.Minute(10), Dates.Millisecond(1))
12 hours, 10 minutes, 30 seconds, 1 millisecond

Dates.format(Dates.Time(z.periods...), "HH:MM:SS")
"12:10:30"

```

It will not work if the time duration exceeds 24 hours. For example, you cannot do this:

```julia
z = Dates.CompoundPeriod(Dates.Day(1))
Dates.format(Dates.Time(z.periods...), "HH:MM:SS") # will error

```
