Time.fromEpochMillis
func Time.fromEpochMillis(ms: int): Time.InstantTime.fromEpochMillis returns the instant ms milliseconds after the Unix epoch, or before it when ms is negative. It panics if the result is beyond the range of an instant, 100 million days each way.
Details
- The Unix epoch is 1970-01-01T00:00:00Z.
- The result is an instant, with no zone. Place it in a zone with
Time.inZoneto read its date and clock time. - The range is ±8,640,000,000,000,000 milliseconds; a count beyond it panics.
Basic examples
Time.fromEpochMillis(0) // 1970-01-01T00:00:00Z
Time.fromEpochMillis(1_793_966_400_000) // 2026-11-06T12:00:00ZA count that is not a whole second keeps its milliseconds:
Time.fromEpochMillis(1_500) // 1970-01-01T00:00:01.5ZScope
A negative count counts back from the epoch:
Time.fromEpochMillis(-1) // 1969-12-31T23:59:59.999Z
Time.fromEpochMillis(-86_400_000) // 1969-12-31T00:00:00ZThe ends of the range are 100 million days from the epoch:
Time.fromEpochMillis(8_640_000_000_000_000) // +275760-09-13T00:00:00Z
Time.fromEpochMillis(-8_640_000_000_000_000) // -271821-04-20T00:00:00ZPass it as a function value:
[0, 60_000, 3_600_000].map(Time.fromEpochMillis) // [1970-01-01T00:00:00Z, 1970-01-01T00:01:00Z, 1970-01-01T01:00:00Z]Named arguments
The parameter is named ms:
Time.fromEpochMillis(ms: 86_400_000) // 1970-01-02T00:00:00ZApplications
Show a timestamp from a JavaScript Date.now() as local time in a zone:
let stockholm = Result.unwrap(Time.zone("Europe/Stockholm"))
Time.fromEpochMillis(1_793_966_400_000).inZone(stockholm) // 2026-11-06T13:00:00+01:00[Europe/Stockholm]Measure the time between two stored timestamps:
let started = Time.fromEpochMillis(1_793_966_400_000)
let finished = Time.fromEpochMillis(1_793_966_490_500)
Time.until(started, finished) // PT90.5SPrint a log of events stored with millisecond timestamps, oldest first:
type Event = { name: string, at: int }
let events = [
Event{ name: "deploy", at: 1_793_966_400_000 },
Event{ name: "build", at: 1_793_962_800_000 },
]
for e in events.sortBy({ x.at }) {
println("${Time.fromEpochMillis(e.at)} ${e.name}")
}2026-11-06T11:00:00Z build
2026-11-06T12:00:00Z deployProperties and relations
Time.epochMillis reverses it, and Time.fromEpochNanos gives the same instant for a million times the count:
for ms in [0, 1, -1, 1_793_966_400_123, -8_640_000_000_000_000] {
let t = Time.fromEpochMillis(ms)
assert(t.epochMillis() == ms)
}
for ms in [0, -1, 1_793_966_400_123] {
assert(Time.fromEpochMillis(ms) == Time.fromEpochNanos(ms * 1_000_000))
}A larger count is a later instant:
Time.compare(Time.fromEpochMillis(1), Time.fromEpochMillis(2)) // -1Possible issues
A count in seconds, as Unix tools print it, lands in January 1970. Multiply by 1000 first:
Time.fromEpochMillis(1_793_966_400) // 1970-01-21T18:19:26.4Z
Time.fromEpochMillis(1_793_966_400 * 1_000) // 2026-11-06T12:00:00ZA count in nanoseconds is far beyond the range, and panics. Use Time.fromEpochNanos for it:
Time.fromEpochMillis(1_793_966_400_000_000_000)
// panic: Time.fromEpochMillis: outside the range of time values
Time.fromEpochNanos(1_793_966_400_000_000_000) // 2026-11-06T12:00:00ZThe count is an int, and a float64 does not convert to it on its own:
let t = Time.fromEpochMillis(1.5e12) // error: cannot use float64 where int is expectedConvert with int, which panics on a fraction; truncate first when the count may have one:
Time.fromEpochMillis(int(1.5e12)) // 2017-07-14T02:40:00Z
Time.fromEpochMillis(int(Math.trunc(1_700_000_000_123.7))) // 2023-11-14T22:13:20.123ZSee also
Time.epochMillis, Time.fromEpochNanos, Time.epochNanos, Time.now, Time.inZone, Time.Instant
