Skip to content

formatDate

The formatDate function formats a date according to a specified string pattern, such as 'dd/MM/yyyy' or 'yyyy-MM-dd'.

Syntax

typescript
formatDate(date: Date, pattern?: string): string;

Parameters

NameTypeDescription
dateDateThe date to be formatted
patternstring(Optional) Formatting pattern (e.g., dd/MM/yyyy)

Returns

TypeDescription
stringDate formatted according to the pattern

Examples

typescript
formatDate(new Date("2024-06-10"), 'dd/MM/yyyy');
// => "10/06/2024"

formatDate(new Date("2024-06-10"), 'yyyy-MM-dd');
// => "2024-06-10"

Notes

  • Throws an error if the date is not valid.
  • Useful for displaying dates in different regional formats.

References

Released under the MIT License.