@technobuddha > library > Object > Conversion
Function: toDate()
ts
function toDate(entity: unknown): Date;Defined in: to-date.ts:18
Converts an unknown entity to a Date object.
- If the entity is
nullorundefined, returns an invalidDate(new Date(Number.NaN)). - If the entity is already a
Date, returns it as is. - If the entity is a
stringornumber, attempts to create aDatefrom it. - Otherwise, converts the entity to a string and creates a
Datefrom that string.
Parameters
| Parameter | Type | Description |
|---|---|---|
entity | unknown | The value to convert to a Date. |
Returns
A Date object representing the input, or an invalid Date if conversion is not possible.