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

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…

You could do something like

``````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
``````
1 Like

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.

``````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> tt = Time(now())
10:44:11.663

julia> convert(Minute, tt)
644 minutes
``````