UUID V7 Time Ordered Generation

Generate RFC 9562 UUIDv7 with Unix timestamp random counter and trailing randomness for time-ordered sortable identifiers.

JavascriptData Integrity

Code

javascriptsnippet
1 line
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

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.