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);}Report a problem with this exampleOpens a pre-filled issue on GitHub.