UUID V7 Time Ordered Generation
Generate RFC 9562 UUIDv7 with Unix timestamp random counter and trailing randomness for time-ordered sortable identifiers.
Code
export function uuidV7(){const now=Date.now(),ts=BigInt(now),buf=new Uint8Array(16),view=new DataView(buf.buffer);view.setBigUint64(0,ts<<16n);crypto.getRandomValues(buf.subarray(8));buf[6]=(buf[6]&0x0f)|0x70;buf[8]=(buf[8]&0x3f)|0x80;const hex=[...buf].map(b=>b.toString(16).padStart(2,'0')).join('');return hex.slice(0,8)+'-'+hex.slice(8,12)+'-'+hex.slice(12,16)+'-'+hex.slice(16,20)+'-'+hex.slice(20);}Inside the snippet
Count the function uuidV7 as the entire surface area of UUID V7 Time Ordered Generation.
Points of care
- Uses
crypto, so it needs a browser context — it will not run as-is in Node without shims.
Where you would put it to use
The main signature reads export function uuidV7()
Treat uuidV7 as the API: import it, and ignore the supporting code.
Interactive Tools for This Topic
UUID v7 GeneratorGenerate time-ordered UUID v7 identifiers (RFC 9562) with a 48-bit millisecond timestamp and random tail, ideal for sortable database keys.UUID Version DetectorValidate the 8-4-4-4-12 UUID layout and read its version (v1 to v8) and variant nibbles per RFC 9562, with full field decomposition.Duration ConverterConvert between human durations like 1w2d3h45m and total seconds, hours, and minutes with negative support.Duration HumanizerTurn a number of seconds into a readable duration such as 1 day, 2 hours, 3 minutes, and show the equivalent in minutes and hours.
FAQ
- Are there third-party dependencies?
- No. The dependency list is empty; the code stays inside the standard library. The file defines `uuidV7` at load time.
Report a problem with this exampleOpens a pre-filled issue on GitHub.