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
- An integer representing the month of the date, between 1 and 12.
- Name
day
- Type
- number
- Description
- An integer representing the day of the date, between 1 and 31.
- Name
year
- Type
- number
- Description
- An integer representing the year of the date, between 1900 and 2100.
- Name
locale
- Type
- string (optional)
- Description
- A string representing the locale to use when formatting the date. Defaults to the user's locale.
- Name
options
- Type
- Intl.DateTimeFormatOptions (optional)
- Description
- An object containing additional options to pass to the toLocaleDateString method. See the official MDN docs for more information.
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.