Time.millisecond
func Time.millisecond(t: Time.Timed): intTime.millisecond returns the millisecond within the second of t, 0 to 999.
Details
- The result is the first three digits of the fractional second. The microseconds and nanoseconds below it are dropped, not rounded:
14:03:05.9996gives 999. - On a
Time.ZonedDateTime, it is the millisecond its wall clock shows. A zone offset is a whole number of seconds, so every zone shows the same millisecond for one instant.
Basic examples
Time.millisecond(Time.plainTime(14, 3, 5, millisecond: 123)) // 123
Time.millisecond(Time.plainTime(14, 3, 5)) // 0Call it through . on any value with a time:
let t = Time.parsePlainTime("14:03:05.5").unwrap()
t.millisecond() // 500Scope
A Time.PlainDateTime has the millisecond of its time:
let pdt = Time.parsePlainDateTime("2026-10-22T14:03:05.999").unwrap()
pdt.millisecond() // 999A Time.ZonedDateTime has the millisecond its wall clock shows:
let z = Time.parseZoned("2026-10-22T16:03:05.250+02:00[Europe/Stockholm]").unwrap()
z.millisecond() // 250Digits below a millisecond belong to Time.microsecond and Time.nanosecond:
let t = Time.parsePlainTime("14:03:05.123456789").unwrap()
t.millisecond() // 123
t.microsecond() // 456
t.nanosecond() // 789A value typed Time.Timed may hold any of the three types with a time. Pass Time.millisecond by name to read each one:
let alarm = Time.plainTime(9, millisecond: 7)
let meeting = Time.parsePlainDateTime("2026-01-06T09:00:00.08").unwrap()
let launch = Time.parseZoned("2026-01-07T12:00:00.9Z[UTC]").unwrap()
let times: Array[Time.Timed] = [alarm, meeting, launch]
times.map(Time.millisecond) // [7, 80, 900]In a pipe, the value is the first argument:
"14:03:05.75" | Time.parsePlainTime() | Result.map(Time.millisecond) // Ok{ value: 750 }Applications
A time renders its fractional second to the last nonzero digit, so 14:03:05.5 and 14:03:05.12 have different widths. Write log stamps with exactly three digits of milliseconds:
func stamp(t: Time.PlainTime): string {
let ms = String.from(t.millisecond()).padStart(3, "0")
"${t.with(millisecond: 0, microsecond: 0, nanosecond: 0)}.${ms}"
}
println(stamp(Time.plainTime(9, 5, 7, millisecond: 42)))
println(stamp(Time.parsePlainTime("14:03:05.5").unwrap()))
println(stamp(Time.plainTime(23, 59, 59, millisecond: 999, microsecond: 999)))09:05:07.042
14:03:05.500
23:59:59.999Keep the events that arrived in the second half of their second:
let hits = ["14:03:05.120", "14:03:05.480", "14:03:06.010", "14:03:06.990"]
| Array.map({ Time.parsePlainTime(x).unwrap() })
println(hits.filter({ x.millisecond() >= 500 }))[14:03:06.99]Properties and relations
The millisecond, microsecond, and nanosecond together are the nanoseconds within the second:
let t = Time.parsePlainTime("14:03:05.123456789").unwrap()
assert(t.millisecond() * 1_000_000 + t.microsecond() * 1000 + t.nanosecond() == 123_456_789)Rounding down to the millisecond with Time.round keeps the millisecond and clears the digits below it:
let t = Time.parsePlainTime("14:03:05.123456789").unwrap()
let floored = t.round(Time.Millisecond{}, mode: Time.Floor{})
assert(floored.millisecond() == t.millisecond())
assert(floored.microsecond() == 0)
assert(floored.nanosecond() == 0)Setting the millisecond with Time.with sets what Time.millisecond reads:
let t = Time.plainTime(14, 3, 5, millisecond: 123)
assert(t.with(millisecond: 0).millisecond() == 0)
assert(t.with(millisecond: 999).millisecond() == 999)The same instant shows the same millisecond in every zone:
let z = Time.parseZoned("2026-10-22T16:03:05.250+02:00[Europe/Stockholm]").unwrap()
let tokyo = z.withZone(Time.zone("Asia/Tokyo").unwrap())
assert(tokyo.millisecond() == z.millisecond())Possible issues
A Time.Instant has no wall clock, so passing one is a compile error:
let instant = Time.fromEpochMillis(1234)
println(Time.millisecond(instant)) // error: cannot use Time.Instant where Time.Timed is expectedPut the instant in a zone first:
let instant = Time.fromEpochMillis(1234)
instant.inZone(Time.utc).millisecond() // 234Time.millisecond drops the digits below it, while the default mode of Time.round rounds half away from zero. Rounding 14:03:05.9996 to the millisecond moves it into the next second:
let t = Time.parsePlainTime("14:03:05.9996").unwrap()
t.millisecond() // 999
t.round(Time.Millisecond{}) // 14:03:06
t.round(Time.Millisecond{}).millisecond() // 0Before 1970, % on the epoch milliseconds is negative, while the millisecond is always 0 to 999:
let t = Time.fromEpochMillis(-1500).inZone(Time.utc)
t.millisecond() // 500
t.epochMillis() % 1000 // -500Time.plainTime panics on a millisecond of 1000 or more, while Time.with constrains it to 999:
let t = Time.plainTime(14, 3, 5)
t.with(millisecond: 1500).millisecond() // 999
Time.plainTime(14, 3, 5, millisecond: 1000) // panic: Time.plainTime: millisecond out of range: 1000See also
Time.second · Time.microsecond · Time.nanosecond · Time.plainTime · Time.with · Time.round · Time.epochMillis · Time.Timed · Time
