to Millisecond Of Day
Returns the time as a millisecond of a day, in 0 until 24 * 60 * 60 * 1_000
.
Note that this is not the number of milliseconds since the start of the day! For example, LocalTime(4, 0).toMillisecondOfDay()
will return 4 * 60 * 60 * 1_000
, the four hours worth of milliseconds, but because of DST transitions, when clocks show 4:00, in fact, three, four, five, or some other number of hours could have passed since the day started. Use Instant to perform reliable time arithmetic.
See also
Samples
import kotlinx.datetime.*
import kotlinx.datetime.format.*
import kotlin.random.*
import kotlin.test.*
fun main() {
//sampleStart
// Obtaining the number of milliseconds a clock has to advance since 00:00 to reach the given time
check(LocalTime(0, 0, 0, 0).toMillisecondOfDay() == 0)
check(LocalTime(0, 0, 0, 1).toMillisecondOfDay() == 0)
check(LocalTime(0, 0, 1, 0).toMillisecondOfDay() == 1000)
check(LocalTime(0, 1, 0, 0).toMillisecondOfDay() == 60_000)
check(LocalTime(1, 0, 0, 0).toMillisecondOfDay() == 3_600_000)
check(LocalTime(1, 1, 1, 1).toMillisecondOfDay() == 3_600_000 + 60_000 + 1_000)
check(LocalTime(1, 1, 1, 1_000_000).toMillisecondOfDay() == 3_600_000 + 60_000 + 1_000 + 1)
//sampleEnd
}
import kotlinx.datetime.*
import kotlinx.datetime.format.*
import kotlin.random.*
import kotlin.test.*
fun main() {
//sampleStart
// Converting a LocalTime to the number of milliseconds since the start of the day and back
val millisecondsInDay = 24 * 60 * 60 * 1_000
val randomNumberOfMilliseconds = Random.nextInt(millisecondsInDay)
val time = LocalTime.fromMillisecondOfDay(randomNumberOfMilliseconds)
check(time.toMillisecondOfDay() == randomNumberOfMilliseconds)
check(time.nanosecond % 1_000_000 == 0) // sub-millisecond part is zero
//sampleEnd
}