Install via yarn add locutus and import:
import { Format } from 'locutus/golang/time/Format'.
Or with CommonJS: const { Format } = require('locutus/golang/time/Format')
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.
exportfunctionFormat(value: DateInput, layout: string): string { // discuss at: https://locutus.io/golang/time/Format // parity verified: Go 1.23 // original by: Kevin van Zonneveld (https://kvz.io) // note 1: Supports a focused subset of Go layout tokens and formats in UTC for deterministic output. // note 1: Unsupported layout markers are treated as literals. // example 1: Format('2026-03-03T13:14:15.000Z', '2006-01-02') // returns 1: '2026-03-03' // example 2: Format('2026-03-03T13:14:15.000Z', '15:04:05') // returns 2: '13:14:15' // example 3: Format('2026-03-03T13:14:15.000Z', '01/02/06 03:04 PM') // returns 3: '03/03/26 01:14 PM' // example 4: Format('2026-03-03T13:14:15.000Z', 'Z07:00') // returns 4: 'Z' // example 5: Format('2026-03-03T13:14:15.000Z', '-07:00') // returns 5: '+00:00'
const date = toUtcDate(value) const resolvedLayout = String(layout) let result = ''
for (let i = 0; i < resolvedLayout.length; ) { const token = isTokenAt(resolvedLayout, i) if (token) { result += formatToken(token, date) i += token.length continue }
const char = resolvedLayout[i] if (char === undefined) { break } result += char i += 1 }
exportfunctionFormat(value, layout) { // discuss at: https://locutus.io/golang/time/Format // parity verified: Go 1.23 // original by: Kevin van Zonneveld (https://kvz.io) // note 1: Supports a focused subset of Go layout tokens and formats in UTC for deterministic output. // note 1: Unsupported layout markers are treated as literals. // example 1: Format('2026-03-03T13:14:15.000Z', '2006-01-02') // returns 1: '2026-03-03' // example 2: Format('2026-03-03T13:14:15.000Z', '15:04:05') // returns 2: '13:14:15' // example 3: Format('2026-03-03T13:14:15.000Z', '01/02/06 03:04 PM') // returns 3: '03/03/26 01:14 PM' // example 4: Format('2026-03-03T13:14:15.000Z', 'Z07:00') // returns 4: 'Z' // example 5: Format('2026-03-03T13:14:15.000Z', '-07:00') // returns 5: '+00:00'
const date = toUtcDate(value) const resolvedLayout = String(layout) let result = ''
for (let i = 0; i < resolvedLayout.length; ) { const token = isTokenAt(resolvedLayout, i) if (token) { result += formatToken(token, date) i += token.length continue }
const char = resolvedLayout[i] if (char === undefined) { break } result += char i += 1 }
return result }
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.
Click "New file" in the appropriate folder
on GitHub.
This will fork the project to your account, directly add the file to it, and send a
Pull Request to us.
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.