# How to convert period in milisecond to minutes / seconds , hour etc

**URL:** <https://discourse.julialang.org/t/how-to-convert-period-in-milisecond-to-minutes-seconds-hour-etc/2423>\
**Category:** New to Julia\
**Tags:** dates, convert\
**Created:** [March 3, 2017, 12:40pm UTC](https://discourse.julialang.org/t/how-to-convert-period-in-milisecond-to-minutes-seconds-hour-etc/2423 "2017-03-03T12:40:45Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![programista](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/programista/32/5372_2.png) [@programista](https://discourse.julialang.org/u/programista)\
**Post date:** [March 3, 2017, 12:40pm UTC](https://discourse.julialang.org/t/how-to-convert-period-in-milisecond-to-minutes-seconds-hour-etc/2423/1 "2017-03-03T12:40:45Z")

</div>

How to convert period in milisecond to minutes / seconds , hour etc  
julia 0.5.0  
julia\> delta=events[1,2].-events[1:1]  
75631000 (miliseconds)

julia\> convert(Dates.Minute,delta)  
75631000 minutes ??? WRONG  
Paul

---

<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:** [March 3, 2017, 1:22pm UTC](https://discourse.julialang.org/t/how-to-convert-period-in-milisecond-to-minutes-seconds-hour-etc/2423/2 "2017-03-03T13:22:57Z")

</div>

Do you need the result to be in a time type? If it’s of some numeric type but represents milliseconds then just do the conversion. `seconds = delta/1000`

If you really need it in a time type then you can extend your [other question](https://discourse.julialang.org/t/how-to-convert-to-datetime-all-column-or-array/2421/2).

```julia
julia> t1 = map((x) -> convert(DateTime,x),events[:,1])
julia> t2 = map((x) -> convert(DateTime,x),events[:,2])
julia> delta = t2 - t1
10-element Array{Base.Dates.Millisecond,1}:
 75631000 milliseconds 
 6031000 milliseconds  
 13531000 milliseconds 
 12685000 milliseconds 
 632000 milliseconds   
 21631000 milliseconds 
 212430000 milliseconds
 30000 milliseconds    
 30000 milliseconds    
 30000 milliseconds 

```

or

```julia
julia> time = map((x) -> convert(DateTime,x),events)
10×2 Array{DateTime,2}:
 2016-04-01T22:00:44 2016-04-02T19:01:15
 2016-04-01T12:00:27 2016-04-01T13:40:58
 2016-04-01T14:43:50 2016-04-01T18:29:21
 2016-04-01T08:29:33 2016-04-01T12:00:58
 2016-04-01T22:53:45 2016-04-01T23:04:17
 2016-04-01T22:53:44 2016-04-02T04:54:15
 2016-04-01T02:59:52 2016-04-03T14:00:22
 2016-04-01T17:26:38 2016-04-01T17:27:08
 2016-04-01T17:32:34 2016-04-01T17:33:04
 2016-04-01T17:24:58 2016-04-01T17:25:28

julia> time[:,2] - time[:,1]
10-element Array{Base.Dates.Millisecond,1}:
 75631000 milliseconds 
 6031000 milliseconds  
 13531000 milliseconds 
 12685000 milliseconds 
 632000 milliseconds   
 21631000 milliseconds 
 212430000 milliseconds
 30000 milliseconds    
 30000 milliseconds    
 30000 milliseconds

```

---

<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:** [March 3, 2017, 4:08pm UTC](https://discourse.julialang.org/t/how-to-convert-period-in-milisecond-to-minutes-seconds-hour-etc/2423/3 "2017-03-03T16:08:58Z")

</div>

I’m not sure what code you’re running or if it’s indeed 0.5, because I get

```julia
julia> convert(Dates.Minute, Dates.Millisecond(75631000))
ERROR: InexactError()
 in divexact(::Int64, ::Int64) at ./dates/periods.jl:376
 in convert(::Type{Base.Dates.Minute}, ::Base.Dates.Millisecond) at ./dates/periods.jl:398

julia> convert(Dates.Millisecond, Dates.Minute(2))
120000 milliseconds

julia> convert(Dates.Minute, convert(Dates.Millisecond, Dates.Minute(2)))
2 minutes

```

---

<div class="post-metadata">

**Author:** ![programista](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/programista/32/5372_2.png) [@programista](https://discourse.julialang.org/u/programista)\
**Post date:** [March 3, 2017, 7:58pm UTC](https://discourse.julialang.org/t/how-to-convert-period-in-milisecond-to-minutes-seconds-hour-etc/2423/4 "2017-03-03T19:58:18Z")

</div>

Nice, :  
julia\> convert(Dates.Minute, Dates.Millisecond(12000\*60))  
12 minutes

bat is unposible :  
julia\> convert(Dates.Minute, Dates.Millisecond(12001\*60))  
ERROR: InexactError()  
in divexact(::Int64, ::Int64) at .\dates\periods.jl:376  
in convert(::Type{Base.Dates.Minute}, ::Base.Dates.Millisecond) at .\dates\periods.jl:398

What with 12001 milliscond ?

> [@quinnj](#):
>
> convert(Dates.Millisecond, Dates.Minute(2))

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [March 3, 2017, 8:13pm UTC](https://discourse.julialang.org/t/how-to-convert-period-in-milisecond-to-minutes-seconds-hour-etc/2423/5 "2017-03-03T20:13:05Z")

</div>

You can also do:

```julia
julia> Dates.CompoundPeriod(Dates.Millisecond(756312342))
1 week, 1 day, 18 hours, 5 minutes, 12 seconds, 342 milliseconds

```

---

<div class="post-metadata">

**Author:** ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)\
**Post date:** [April 15, 2017, 12:17pm UTC](https://discourse.julialang.org/t/how-to-convert-period-in-milisecond-to-minutes-seconds-hour-etc/2423/6 "2017-04-15T12:17:01Z")

</div>

> [@stevengj](#):
>
> julia\> Dates.CompoundPeriod(Dates.Millisecond(756312342))  
> 1 week, 1 day, 18 hours, 5 minutes, 12 seconds, 342 milliseconds

Sadly this appears to have disappeared in v0.6:

```julia
julia> Dates.CompoundPeriod(Dates.Millisecond(756312342))
756312342 milliseconds

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [April 15, 2017, 1:51pm UTC](https://discourse.julialang.org/t/how-to-convert-period-in-milisecond-to-minutes-seconds-hour-etc/2423/7 "2017-04-15T13:51:52Z")

</div>

> [@cormullion](#):
>
> Sadly this appears to have disappeared in v0.6:

In 0.6 you can do

```julia
julia> Dates.canonicalize(Dates.CompoundPeriod(Dates.Millisecond(756312342)))
1 week, 1 day, 18 hours, 5 minutes, 12 seconds, 342 milliseconds

```

---

<div class="post-metadata">

**Author:** ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)\
**Post date:** [April 15, 2017, 2:04pm UTC](https://discourse.julialang.org/t/how-to-convert-period-in-milisecond-to-minutes-seconds-hour-etc/2423/8 "2017-04-15T14:04:06Z")

</div>

> [@stevengj](#):
>
> Dates.canonicalize(Dates.CompoundPeriod(Dates.Millisecond(756312342)))

Cool. That’s a nice function. With a cool name that provides some typing practice… 🙂

---

<div class="post-metadata">

**Author:** ![Karolina7](https://avatars.discourse-cdn.com/v4/letter/k/b782af/32.png) [@Karolina7](https://discourse.julialang.org/u/Karolina7)\
**Post date:** [October 2, 2018, 1:37pm UTC](https://discourse.julialang.org/t/how-to-convert-period-in-milisecond-to-minutes-seconds-hour-etc/2423/9 "2018-10-02T13:37:35Z")

</div>

#milliseconds to seconds:  
delta\_sec = Dates.Second(delta)

#milliseconds to minutes:  
delta\_min = Dates.Minute(delta)

---

<div class="post-metadata">

**Author:** ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)\
**Post date:** [December 4, 2018, 2:22pm UTC](https://discourse.julialang.org/t/how-to-convert-period-in-milisecond-to-minutes-seconds-hour-etc/2423/10 "2018-12-04T14:22:47Z")

</div>

For my own future reference (and anyone else who might stuble across this when googling).

In 1.0 it is written type last.

```julia
julia> round(Millisecond(12_001*60), Minute)
12 minutes

```

The logic behind this is that your are also allowed to specify things like

```julia
julia> round(Millisecond(12_001*60), Minute(5))
10 minutes

```

See discussion on

> <https://github.com/JuliaLang/julia/pull/24182>
>
> Allows (most) subtypes of \`Period\` to be rounded with \`floor\`, \`ceil\`, and \`roun…d\`, also allowing for easy conversion between \`Period\` subtypes where we would otherwise encounter an \`InexactError\`.
> 
> \`\`\`julia
> julia\> x = Dates.Millisecond(29394943)
> 29394943 milliseconds
> 
> julia\> convert(Dates.Hour, x)
> ERROR: InexactError: divexact(Int64, 8.165261944444444)
> Stacktrace:
> \[1\] divexact(::Int64, ::Int64) at ./dates/periods.jl:400
> \[2\] convert(::Type{Base.Dates.Hour}, ::Base.Dates.Millisecond) at ./dates/periods.jl:422
> 
> julia\> round(x, Dates.Hour)
> 8 hours
> \`\`\`
> 
> As with the date rounding functions, this approach also supports rounding to values (e.g., the nearest 15 minutes) as well as types:
> 
> \`\`\`julia
> julia\> x = Dates.Hour(910)
> 910 hours
> 
> julia\> ceil(x, Dates.Hour(12))
> 912 hours
> \`\`\`
> 
> I can see at least two potential concerns being raised about this approach:
> 1. As discussed in https://github.com/JuliaLang/julia/issues/18574, there may be some confusion/contention surrounding the typical rounding signature (\`round(value, precision)\`) and the convert signature (\`convert(type, value)\`).
> 2. ~For certain very large values, the rounding may still result in an \`InexactError\`. This is because all values are converted to \`Nanosecond\` for rounding, so we can't easily accommodate values that exceed \`typemax(Int64)\` nanoseconds (which is a little over 292 years). This could be mitigated by using a lower-precision \`Period\` type when nanosecond precision is not required.~
> 
> \*\*Edit:\*\* Point 2 above has been mitigated via \`promote\`.

---

<div class="post-metadata">

**Author:** ![bernhard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bernhard/32/2619_2.png) [@bernhard](https://discourse.julialang.org/u/bernhard)\
**Post date:** [October 8, 2019, 11:48am UTC](https://discourse.julialang.org/t/how-to-convert-period-in-milisecond-to-minutes-seconds-hour-etc/2423/11 "2019-10-08T11:48:19Z")

</div>

Do you know a canonical way of converting a compound period to (e.g.) minutes (with digits)

```julia
julia> t=Minute(3)+Second(26)
3 minutes, 26 seconds

julia> want=3+26/60
3.433333333333333

```

I suppose I can always (??) convert to something small (say Nanosecond or Millisecond) and then divide, but there must be a more elegant way (or rather, an existing function) for this.

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [October 8, 2019, 11:56am UTC](https://discourse.julialang.org/t/how-to-convert-period-in-milisecond-to-minutes-seconds-hour-etc/2423/12 "2019-10-08T11:56:09Z")

</div>

I’d do:

```julia
DateTime(0) + t - DateTime(0)

```

but I’m also curious to see if there’s a better way…

---

<div class="post-metadata">

**Author:** ![bernhard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bernhard/32/2619_2.png) [@bernhard](https://discourse.julialang.org/u/bernhard)\
**Post date:** [October 8, 2019, 12:00pm UTC](https://discourse.julialang.org/t/how-to-convert-period-in-milisecond-to-minutes-seconds-hour-etc/2423/13 "2019-10-08T12:00:07Z")

</div>

ok, I guess this works (as for elegance…)

```julia
julia> t=Minute(3)+Second(26)
3 minutes, 26 seconds

julia> (DateTime(0) + t - DateTime(0))/convert(Millisecond,Minute(1))
3.433333333333333

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [October 8, 2019, 12:49pm UTC](https://discourse.julialang.org/t/how-to-convert-period-in-milisecond-to-minutes-seconds-hour-etc/2423/14 "2019-10-08T12:49:18Z")

</div>

You can do

```julia
Dates.toms(t) / Dates.toms(Minute(1))

```

using the undocumented `Dates.toms` function to convert time periods to floating-point milliseconds.

---

<div class="post-metadata">

**Author:** ![TheLateKronos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thelatekronos/32/12824_2.png) [@TheLateKronos](https://discourse.julialang.org/u/TheLateKronos)\
**Post date:** [February 17, 2022, 12:56pm UTC](https://discourse.julialang.org/t/how-to-convert-period-in-milisecond-to-minutes-seconds-hour-etc/2423/15 "2022-02-17T12:56:21Z")

</div>

This seems to be undocumented:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/8/8/8812f7851860f4d606a28093659b0911684cd3ee.png)

Should it perhaps be documented under " [Conversion Functions](https://docs.julialang.org/en/v1/stdlib/Dates/#Conversion-Functions)"?

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [February 17, 2022, 1:25pm UTC](https://discourse.julialang.org/t/how-to-convert-period-in-milisecond-to-minutes-seconds-hour-etc/2423/16 "2022-02-17T13:25:47Z")

</div>

Also see (my) package `DateFormats.jl`. It converts between different date/time formats, including regular decimal numbers.

That is, instead of

> [@stevengj](#):
>
> `Dates.toms(t) / Dates.toms(Minute(1))`

do `period_decimal(Minute, t)`.  
For example,

```julia
using DateFormats

julia> period_decimal(Minute, Second(123))
2.05

# and inverse conversion
julia> period_decimal(Minute, 2.05) == Second(123)
true

```

Note that, unlike `Dates.toms`, it doesn’t truncate everything below 1 ms to zero.
