Skip to content

getDateFromUlid

function getDateFromUlid(__namedParameters): Date | null;

Defined in: ulid-utils.ts:201

Read the creation time embedded in a prefixed or plain ULID, as a Date. Handy as a coarse “created at” for debugging and sorting; don’t use it in place of a stored timestamp.

Parameter Type
__namedParameters { id: string; throwOnInvalid?: boolean; }
__namedParameters.id string
__namedParameters.throwOnInvalid? boolean

Date | null

The Date, or null when the id isn’t a valid ULID. With throwOnInvalid: true, any invalid id throws instead. A ULID whose timestamp is 0 ("00000000000000000000000000") is valid and gives the epoch.

import { getDateFromUlid } from "@xtandard/lib/ulid";
getDateFromUlid({ id: "usr_01jd8x3kq6f2v9w4z7y5b8c1ne" }); // Date 2024-11-22T03:02:04.774Z
getDateFromUlid({ id: "usr_hello" }); // null