Skip to content

convertDateTo

The convertDateTo function converts a date to a specific output type: string, number, or Date object.

Syntax

typescript
convertDateTo<T extends 'string' | 'number' | 'date'>(date: Date, type: T): string | number | Date;

Parameters

NameTypeDescription
dateDateThe date to be converted
typestringThe desired output type: 'string', 'number', or 'date'

Returns

TypeDescription
string | number | DateValue converted according to the requested type

Examples

typescript
convertDateTo(new Date("2024-06-10T12:00:00Z"), 'string');
// => "2024-06-10T12:00:00.000Z"

convertDateTo(new Date("2024-06-10T12:00:00Z"), 'number');
// => 1718020800000

convertDateTo(new Date("2024-06-10T12:00:00Z"), 'date');
// => Date Mon Jun 10 2024 12:00:00 GMT+0000

Notes

  • Throws an error if the date is not valid or if the type is invalid.
  • Useful for standardizing date output in different contexts.

References

Released under the MIT License.