Go's time.UnixMilli in TypeScript

✓ Verified: Go 1.23
Examples tested against actual runtime. CI re-verifies continuously. Only documented examples are tested.

How to use

Install via yarn add locutus and import: import { UnixMilli } from 'locutus/golang/time/UnixMilli'.

Or with CommonJS: const { UnixMilli } = require('locutus/golang/time/UnixMilli')

Use a bundler that supports tree-shaking so you only ship the functions you actually use. Vite, webpack, Rollup, and Parcel all handle this. For server-side use this is less of a concern.

Examples

These examples are extracted from test cases that automatically verify our functions against their native counterparts.

#codeexpected result
1UnixMilli(0).toISOString()'1970-01-01T00:00:00.000Z'
2UnixMilli(1700000000123).toISOString()'2023-11-14T22:13:20.123Z'
3UnixMilli(-1).toISOString()'1969-12-31T23:59:59.999Z'

Notes

  • Returns a JavaScript Date for milliseconds since Unix epoch.

Here's what our current TypeScript equivalent to Go's time.UnixMilli looks like.

export function UnixMilli(msec: number): Date {
// discuss at: https://locutus.io/golang/time/UnixMilli
// parity verified: Go 1.23
// original by: Kevin van Zonneveld (https://kvz.io)
// note 1: Returns a JavaScript Date for milliseconds since Unix epoch.
// example 1: UnixMilli(0).toISOString()
// returns 1: '1970-01-01T00:00:00.000Z'
// example 2: UnixMilli(1700000000123).toISOString()
// returns 2: '2023-11-14T22:13:20.123Z'
// example 3: UnixMilli(-1).toISOString()
// returns 3: '1969-12-31T23:59:59.999Z'

const millis = Math.trunc(Number(msec))
if (!Number.isFinite(millis)) {
throw new TypeError('UnixMilli(): invalid timestamp')
}

return new Date(millis)
}

Improve this function

Locutus is a community effort following The McDonald's Theory: we ship first iterations, hoping others will improve them. If you see something that could be better, we'd love your contribution.

View on GitHub · Edit on GitHub · View Raw


We have 47 Go functions so far - help us add more

Got a rainy Sunday afternoon and a taste for a porting puzzle?

We will then review it. If it's useful to the project and in line with our contributing guidelines your work will become part of Locutus and you'll be automatically credited in the authors section accordingly.


« More Go time functions


Star