A collection of functions to format data.
A chainable interface to manipulate the options passed to Intl.DateTimeFormat.
This module offers three apis to manipulate the options passed to Intl.DateTimeFormat. Each api offers different affordances to manipulate the options.
formatDate(date, options, locale)
: The most basic api. It takes a date and options and returns the formatted date string.createFormatDate(options, locale)
: A chainable api that allows you to manipulate the options. offers a lot of shorthand functions to manipulate the options.[option](date)
: A set of functions that callcreateFormatDate()
without any option, turns on a single option, then callformat()
with the date passed to the function. Those function are also available for imports.
The module also exports single functions to quickly format a date:
Gets the short format of the date, in the default timezone and current locale.
short(date: Date, options: FormatDateOptions = {}): string
Example Output:
const formattedDate = short(new Date()); // "4/11/2024"
Gets the medium format of the date, in the default timezone and current locale.
medium(date: Date, options: FormatDateOptions = {}): string
Example Output:
const formattedDate = medium(new Date()); // "Apr 11, 2024"
Gets the long format of the date, in the default timezone and current locale.
long(date: Date, options: FormatDateOptions = {}): string
Example Output:
const formattedDate = long(new Date()); // "April 11, 2024"
Gets the full format of the date, in the default timezone and current locale.
full(date: Date, options: FormatDateOptions = {}): string
Example Output:
const formattedDate = full(new Date()); // "Monday, April 11, 2024"
Gets the numeric year of the date, in the default timezone and current locale.
year(date: Date, options: FormatDateOptions = {}): string
Example Output:
const formattedYear = year(new Date()); // "2024"
Gets the 2-digit month of the date, in the default timezone and current locale.
month(date: Date, options: FormatDateOptions = {}): string
Example Output:
const formattedMonth = month(new Date()); // "04"
Gets the numeric month of the date, in the default timezone and current locale.
monthNumeric(date: Date, options: FormatDateOptions = {}): string
Example Output:
const formattedMonth = monthNumeric(new Date()); // "4"
Gets the long month of the date, in the default timezone and current locale.
monthName(date: Date, options: FormatDateOptions = {}): string
Example Output:
const formattedMonthName = monthName(new Date()); // "April"
Gets the 2-digit day of the date, in the default timezone and current locale.
day(date: Date, options: FormatDateOptions = {}): string
Example Output:
const formattedDay = day(new Date()); // "11"
Gets the numeric day of the date, in the default timezone and current locale.
dayNumeric(date: Date, options: FormatDateOptions = {}): string
Example Output:
const formattedDay = dayNumeric(new Date()); // "11"
Gets the long weekday of the date, in the default timezone and current locale.
weekday(date: Date, options: FormatDateOptions = {}): string
Example Output:
const formattedWeekday = weekday(new Date()); // "Monday"
Gets the 2-digit hour of the date, in the default timezone and current locale.
hour(date: Date, options: FormatDateOptions = {}): string
Example Output:
const formattedHour = hour(new Date()); // "06"
Gets the numeric hour of the date, in the default timezone and current locale.
hourNumeric(date: Date, options: FormatDateOptions = {}): string
Example Output:
const formattedHour = hourNumeric(new Date()); // "6"
Gets the 2-digit minutes of the date, in the default timezone and current locale.
minute(date: Date, options: FormatDateOptions = {}): string
Example Output:
const formattedMinute = minute(new Date()); // "09"
Gets the numeric minutes of the date, in the default timezone and current locale.
minuteNumeric(date: Date, options: FormatDateOptions = {}): string
Example Output:
const formattedMinute = minuteNumeric(new Date()); // "9"
Gets the 2-digit seconds of the date, in the default timezone and current locale.
second(date: Date, options: FormatDateOptions = {}): string
Example Output:
const formattedSecond = second(new Date()); // "12"
Gets the numeric seconds of the date, in the default timezone and current locale.
secondNumeric(date: Date, options: FormatDateOptions = {}): string
Example Output:
const formattedSecond = secondNumeric(new Date()); // "12"
Gets the short time of the date, in the default timezone and current locale.
time(date: Date, options: FormatDateOptions = {}): string
Example Output:
const formattedTime = time(new Date()); // "06:09 AM"
Gets the short time of the date in 24h format, in the default timezone.
time24h(date: Date, options: FormatDateOptions = {}): string
Example Output:
const formattedTime = time24h(new Date()); // "06:09"
Formats the date into the ISO format, in the default timezone.
yyyymmdd(date: Date, options: FormatDateOptions = {}): string
Example Output:
const formattedDate = yyyymmdd(new Date()); // "2024-04-11"
Formats the date into the ISO format, in the USER'S timezone.
yyyymmddLocal(date: Date, options: FormatDateOptions = {}): string
Example Output:
const formattedDate = yyyymmddLocal(new Date()); // "2024-04-11"
This modules exports a formatPrice
wrapper around Intl.NumberFormat that returns the formatted
price string.
Formats a duration in minute into something more easier to read.
Makes it easy to format and display sizes in bytes in a human friendly way. It provides a chain-able api for the limit and precision options.
This module provides functions to format a date relative to now. It uses the Intl.RelativeTimeFormat
API, which is not supported by all browsers. It will default to toLocaleString()
if the API is not
supported or an error occurs.
The major feature in this module is the ability to detect the proper unit to use based on the
difference between the date and now. For example, if the difference is 1 day, it will use the day
unit, but if the difference is 1 hour, it will use the hour
unit. This is done by providing a list
of units to use, and the maximum difference for each unit. The units are also skewed to make the
difference more human readable. For example, the day
unit will be used even if the difference is
less than 1 day, but close to it.
It supports 3 api:
formatRelativeTime(date, options, unit, locale, now)
: formats the date relative to now, using the given options, unit and locale. You can also set thenow
date, which defaults tonew Date()
.relativeTime(options, unit, locale, now)
: creates a formatter for the given options, unit, locale and now date. It returns a chainable object that allows you to set the unit, locale and now date, and then format a date.timeAgo(date)
: formats the date relative to now, using the default options, unit and locale. This is probably the most common use case, so it's provided as a convenience.
The translations needed for humanDuration
, humanSize
and relativeTime
.