https://kotlinlang.org logo
#getting-started
Title
# getting-started
j

Jan

04/08/2022, 4:42 PM
Hey I was using this approach: (thread) to split my seconds into minutes and seconds. However if I get seconds > 3600 this one doesn't work anymore as seen in the picture. Anyone got a better solution?
grafik.png
(sum is the total seconds)
seconds = 13
a

akatkov

04/08/2022, 4:45 PM
val newMinutes = sum / 60
would get you the number of minutes, but it looks like you’re trying to keep track of the number of hours as well
j

Jan

04/08/2022, 4:46 PM
didn't want to before but I think I'll just add hours to it
is probably the easiest way
a

akatkov

04/08/2022, 4:46 PM
the number of seconds left over after splitting as many seconds as possible into hours and minutes would be
val seconds = ((seconds % 3600) % 60)
c

Casey Brooks

04/08/2022, 4:47 PM
Converting it to a Duration will probably be the easiest, let the stblib do the math for you.
Copy code
val sumDuration: Duration = sum.seconds
        val totalHours: Long = sumDuration.inWholeHours
        val totalMinutes: Long = sumDuration.inWholeMinutes
        val totalSeconds: Long = sumDuration.inWholeSeconds
1
👍 2
j

Jan

04/08/2022, 4:51 PM
thats true thanks
c

Casey Brooks

04/08/2022, 5:02 PM
There’s a lot of helper functions to do whatever you need with the Duration. The simplest is just calling
.toString()
to get a nicely-formatted, human-readable duration. The
.toComponents { }
functions split the value out into the component parts if you need to do something else with them
3
4 Views