# Convert "Time of the day" to Total Minute or Total Second

**URL:** <https://discourse.julialang.org/t/convert-time-of-the-day-to-total-minute-or-total-second/33093>\
**Category:** New to Julia\
**Created:** [January 8, 2020, 10:14am UTC](https://discourse.julialang.org/t/convert-time-of-the-day-to-total-minute-or-total-second/33093 "2020-01-08T10:14:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eric\_Chen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eric_chen/32/5992_2.png) [@Eric\_Chen](https://discourse.julialang.org/u/Eric_Chen)\
**Post date:** [January 8, 2020, 10:14am UTC](https://discourse.julialang.org/t/convert-time-of-the-day-to-total-minute-or-total-second/33093/1 "2020-01-08T10:14:35Z")

</div>

tt = Time(now()) → give me a time, e.g. 2 hour, 10 minute, 5 second,

What is the most efficient / elegant way to convert it to Total Minutes? i.e. 2 \* 60 + 10 = 130 (either Int or Float or Minute)

Of coz I can manually do Minute(tt) + 60\* Hour(tt), but it is not generic enough…  
Because I need to write a seperate function to get Total Hours, Total Second, Total Milisecond etc…

---

<div class="post-metadata">

**Author:** ![ianfiske](https://avatars.discourse-cdn.com/v4/letter/i/58f4c7/32.png) [@ianfiske](https://discourse.julialang.org/u/ianfiske)\
**Post date:** [January 8, 2020, 2:20pm UTC](https://discourse.julialang.org/t/convert-time-of-the-day-to-total-minute-or-total-second/33093/2 "2020-01-08T14:20:06Z")

</div>

You could do something like

```julia
julia> current_time_length(x, period) = Dates.toms(x - floor(x, Day))/Dates.toms(period)
current_time_length (generic function with 1 method)

julia> current_time_length(now(), Second(1))
33473.864

julia> current_time_length(now(), Hour(1))
9.315835

```

---

<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:** [January 8, 2020, 3:50pm UTC](https://discourse.julialang.org/t/convert-time-of-the-day-to-total-minute-or-total-second/33093/3 "2020-01-08T15:50:11Z")

</div>

Internally, `Time` values are kept as a number of `Nanosecond`s, and, internally, `Nanoseconds` are kept as `Int64` values. Assuming you want the corresponding complete (whole) `Minute`s, integer division `div` is the appropriate operation. The `Dates` package uses `Minute(x::Time)` to isolate the minutes from e.g the hours, the seconds of a time. We do not want to override that; using `Base.convert` is a clean approach because `Dates` does not define that and the meaning matches the action.

```julia
using Dates

const NanosecondsPerSecond = 1_000_000_000
const NanosecondsPerMinute = 60 * NanosecondsPerSecond 

Base.convert(::Type{Minute}, x::Time) = 
  Minute(div(x.instant.value, NanosecondsPerMinute))

```

with that

```julia
julia> tt = Time(now())
10:44:11.663

julia> convert(Minute, tt)
644 minutes

```
