@@ -255,6 +255,51 @@ try {
255255}
256256```
257257
258+ ### 6. UUID v7 Timestamp Extraction
259+
260+ ``` typescript
261+ import { parseUUID , extractTimestampFromV7 , formatUUID } from ' @usex/uuidv47' ;
262+
263+ // Extract timestamp from a UUID v7
264+ const v7UUID = parseUUID (" 018f4e7c-3c4a-7000-8000-123456789abc" );
265+ const timestamp = extractTimestampFromV7 (v7UUID );
266+
267+ console .log (` UUID v7: ${formatUUID (v7UUID )} ` );
268+ console .log (` Timestamp: ${timestamp .getTime ()} ms ` );
269+ console .log (` Date: ${timestamp .toISOString ()} ` ); // "2024-10-07T23:15:45.610Z"
270+ console .log (` Local: ${timestamp .toLocaleString ()} ` );
271+
272+ // Compare creation times of multiple UUIDs
273+ const uuid1 = parseUUID (" 018f4e7c-3c4a-7000-8000-111111111111" );
274+ const uuid2 = parseUUID (" 018f4e7c-4000-7000-8000-222222222222" );
275+ const uuid3 = parseUUID (" 018f4e7c-5000-7000-8000-333333333333" );
276+
277+ const date1 = extractTimestampFromV7 (uuid1 );
278+ const date2 = extractTimestampFromV7 (uuid2 );
279+ const date3 = extractTimestampFromV7 (uuid3 );
280+
281+ console .log (" UUIDs in chronological order:" );
282+ console .log (` ${formatUUID (uuid1 )}: ${date1 .toISOString ()} ` );
283+ console .log (` ${formatUUID (uuid2 )}: ${date2 .toISOString ()} ` );
284+ console .log (` ${formatUUID (uuid3 )}: ${date3 .toISOString ()} ` );
285+
286+ // Use for sorting, filtering by date, etc.
287+ const uuids = [uuid3 , uuid1 , uuid2 ];
288+ const sortedByTime = uuids .sort ((a , b ) => {
289+ const timeA = extractTimestampFromV7 (a ).getTime ();
290+ const timeB = extractTimestampFromV7 (b ).getTime ();
291+ return timeA - timeB ;
292+ });
293+
294+ // Error handling - only works with v7 UUIDs
295+ try {
296+ const v4UUID = parseUUID (" a1b2c3d4-e5f6-4000-8000-123456789abc" );
297+ extractTimestampFromV7 (v4UUID );
298+ } catch (error ) {
299+ console .log (error .message ); // "Cannot extract timestamp: UUID is version 4, expected version 7"
300+ }
301+ ```
302+
258303## 📚 API Reference
259304
260305### Core Transformation Functions
@@ -395,6 +440,38 @@ const isValid = isValidUUIDString("018f4e7c-3c4a-7000-8000-123456789abc"); // tr
395440const isInvalid = isValidUUIDString (" invalid-uuid" ); // false
396441```
397442
443+ #### ` extractTimestampFromV7(uuid: UUID128): Date `
444+ Extract the timestamp from a UUID v7 and convert it to a JavaScript Date object.
445+
446+ UUID v7 stores a 48-bit Unix timestamp (milliseconds since epoch) in the first 6 bytes. This function extracts that timestamp and returns it as a Date object.
447+
448+ ``` typescript
449+ import { extractTimestampFromV7 , parseUUID } from ' @usex/uuidv47' ;
450+
451+ // Parse a UUID v7
452+ const uuid = parseUUID (" 018f4e7c-3c4a-7000-8000-123456789abc" );
453+
454+ // Extract the timestamp
455+ const date = extractTimestampFromV7 (uuid );
456+ console .log (date .toISOString ()); // "2024-10-07T23:15:45.610Z"
457+ console .log (date .getTime ()); // 1728340545610
458+
459+ // Compare timestamps from multiple UUIDs
460+ const uuid1 = parseUUID (" 018f4e7c-3c4a-7000-8000-123456789abc" );
461+ const uuid2 = parseUUID (" 018f4e7c-4000-7000-8000-123456789abc" );
462+ const date1 = extractTimestampFromV7 (uuid1 );
463+ const date2 = extractTimestampFromV7 (uuid2 );
464+ console .log (date1 < date2 ); // true (uuid1 was created before uuid2)
465+ ```
466+
467+ ** Note:** This function will throw an error if the UUID is not version 7.
468+
469+ ``` typescript
470+ // This will throw an error
471+ const v4UUID = parseUUID (" 01234567-89ab-4000-8000-123456789abc" );
472+ extractTimestampFromV7 (v4UUID ); // Error: Cannot extract timestamp: UUID is version 4, expected version 7
473+ ```
474+
398475### UUID Version and Variant Functions
399476
400477#### ` getUUIDVersion(uuid: UUID128): UUIDVersion `
0 commit comments