Time-based UUIDs with uuid v7
A version 7 UUID starts with a Unix timestamp in milliseconds, so ids created later carry a later time.
How it works
- Call v7(). Import
v7and call it five times. The example then sorts the strings and checks the sorted list against the order they were created in. [1] - Set the time with msecs. The
msecsoption sets the timestamp field. The helpermsFromV7reads the first 12 hex digits back into a date, so you can see 2025-01-01 come back out. [1] - Calls with and without options. The first five calls pass no options; the last one passes
{ msecs }. The README describes how the two cases are handled. [1]
Run and edit it
It runs in your browser, in a sandboxed frame that cannot see this site; the packages load from esm.sh only after you press Run.
import { v7 as uuidv7 } from 'uuid'
// Generate a few v7 UUIDs in a row
const ids = []
for (let i = 0; i < 5; i++) ids.push(uuidv7())
ids.forEach((id, i) => console.log(`v7 #${i + 1}:`, id))
// Sort the strings and compare with creation order
const sorted = [...ids].sort()
console.log('sorted order equals creation order:', sorted.every((id, i) => id === ids[i]))
// The first 12 hex digits hold the millisecond timestamp
const msFromV7 = (id) => parseInt(id.replace(/-/g, '').slice(0, 12), 16)
console.log('time in v7 #1:', new Date(msFromV7(ids[0])).toISOString())
// msecs option sets the timestamp field yourself
const fixed = uuidv7({ msecs: Date.UTC(2025, 0, 1) })
console.log('v7 for 2025-01-01:', fixed)
console.log('decoded back:', new Date(msFromV7(fixed)).toISOString())
The decoded time of v7 #1 is the moment you ran the code; change the date in Date.UTC to move the fixed one. Code written by AgentGid [1].
The decoded time of v7 #1 is the moment you ran the code; change the date in Date.UTC to move the fixed one. Code written by AgentGid [1].Checked Oct 12, 2026: it starts without errors in Chromium with uuid 14.0.3. Needs WebGL.
More uuid examples
Random UUIDs with uuid v4 Name-based UUIDs with uuid v5 Validating UUIDs with uuid Converting UUIDs to bytes with uuid
Sources
Explanations written by AgentGid from the official docs; the numbers in brackets point here. The code marked "written by AgentGid" is ours and free to copy.