Skip to main content

formatDate

The formatDate function formats a date in a locale-specific format. Under the hood, it's basically only calling the Date.prototype.toLocaleDateString method with improved user-error handling.

Usage example

import { formatDate } from 'calendar-widgets';

const formattedDate = formatDate(4, 9, 2023);
console.log(formattedDate); // Output: 04/09/2023

Parameters

Name
month
Type
number
Description
Name
day
Type
number
Description
Name
year
Type
number
Description
Name
locale
Type
string (optional)
Description
Name
options
Type
Intl.DateTimeFormatOptions (optional)
Description

Return Value

The function returns a string representing the formatted & localized date.

Errors

The function throws an error if any of the following conditions are met:

  • The month parameter is not an integer between 1 and 12.
  • The day parameter is not an integer between 1 and 31.
  • The year parameter is not an integer between 1900 and 2100.
  • The locale parameter is a string.