Time and date formatter class.
The main aim of this class is to make it easy to produce a date which is consistent with the way server-side MediaWiki formats dates, in the current user's timezone and preferred format.
The input format is always a native Date object. This isn't a date parser class.
The most common operations are available as static methods, which have no dependency on "this", so they can be called via destructuring assignment.
The constructor is internal. Instances should be obtained via the static factory methods.
Example
// Static interface
const { formatTimeAndDate } = require( 'mediawiki.DateFormatter' );
const now = formatTimeAndDate( new Date() );
// Non-static interface
const DateFormatter = require( 'mediawiki.DateFormatter' );
const mwTimestamp = DateFormatter.forUtc().formatMw( new Date() );
Methods
formatDate(date) → {string}
#
Format a Date to a date string (non-static)
Parameters:
| Name | Type | Description |
|---|---|---|
date |
Date |
Returns:
- Type
- string
formatDateRange(date1, date2) → {string}
#
Format a date range, showing date only. (non-static)
TODO: Have MW provide localisation.
Parameters:
Returns:
- Type
- string
formatForDateTimeInput(date) → {string}
#
Format a Date in ISO 8601 format, without the timezone designator, implying unqualified local time, and without seconds. This is suitable for passing to a datetime-local input. (non-static)
Parameters:
| Name | Type | Description |
|---|---|---|
date |
Date |
Returns:
- Type
- string
Format a Date in ISO 8601 format, without the timezone designator, implying unqualified local time, and without seconds.
formatIso(date) → {string}
#
Format a Date in ISO 8601 format, including timezone designator. (non-static)
Parameters:
| Name | Type | Description |
|---|---|---|
date |
Date |
Returns:
- Type
- string
formatMw(date) → {string}
#
Format a Date to a MediaWiki 14-character timestamp. Since MediaWiki timestamps are conventionally UTC, this should typically be called on an instance retrieved with forUtc().
Parameters:
| Name | Type | Description |
|---|---|---|
date |
Date |
Returns:
- Type
- string
formatPrettyDate(date) → {string}
#
Format a Date to a date string without the year (non-static)
Parameters:
| Name | Type | Description |
|---|---|---|
date |
Date |
Returns:
- Type
- string
formatTime(date) → {string}
#
Format a Date to a time string (non-static)
Parameters:
| Name | Type | Description |
|---|---|---|
date |
Date |
Returns:
- Type
- string
formatTimeAndDate(date) → {string}
#
Format a Date to a time and date string (non-static)
Parameters:
| Name | Type | Description |
|---|---|---|
date |
Date |
Returns:
- Type
- string
formatTimeAndDateRange(date1, date2) → {string}
#
Format a date range, showing time and date. (non-static)
TODO: Have MW provide localisation. This function depends on the browser for localisation. It does not properly respect the user's date format preferences, and has limited language support.
Parameters:
Returns:
- Type
- string
formatTimeRange(date1, date2) → {string}
#
Format a date range, showing time only. (non-static)
TODO: Have MW provide localisation.
Parameters:
Returns:
- Type
- string
getShortZoneName(date) → {string}
#
Get the short name of the time zone, e.g. "PST".
Parameters:
| Name | Type | Description |
|---|---|---|
date |
Date | The reference date |
Returns:
- Type
- string
getZoneOffsetMinutes(date) → {number}
#
Get the time zone offset as a number of minutes, in the sign-preserving convention, e.g. "+01:00" becomes 60.
Parameters:
| Name | Type | Description |
|---|---|---|
date |
Date | The reference date for DST |
Returns:
- Type
- number
Get the time zone offset as a number of minutes, in the sign-preserving convention, e.g.
forSiteZone() → {module:mediawiki.DateFormatter}static
#
Get a DateFormatter instance, configured for the current user except with the site default time zone.
Returns:
Get a DateFormatter instance, configured for the current user except with the site default time zone.
forUser() → {module:mediawiki.DateFormatter}static
#
Get a DateFormatter instance configured for the current user
Returns:
forUtc() → {module:mediawiki.DateFormatter}static
#
Get a DateFormatter instance, configured for the current user except with the time zone set to UTC.
Returns:
Get a DateFormatter instance, configured for the current user except with the time zone set to UTC.
formatDate(date) → {string}static
#
Format a Date to a date string (static variant)
Parameters:
| Name | Type | Description |
|---|---|---|
date |
Date |
Returns:
- Type
- string
formatDateRange(date1, date2) → {string}static
#
Format a date range, showing date only. (static variant)
TODO: Have MW provide localisation.
Parameters:
Returns:
- Type
- string
formatForDateTimeInput(date) → {string}static
#
Format a Date in ISO 8601 format, without the timezone designator, implying unqualified local time, and without seconds. This is suitable for passing to a datetime-local input. (static variant)
Parameters:
| Name | Type | Description |
|---|---|---|
date |
Date |
Returns:
- Type
- string
Format a Date in ISO 8601 format, without the timezone designator, implying unqualified local time, and without seconds.
formatIso(date) → {string}static
#
Format a Date in ISO 8601 format, including timezone designator. (static variant)
Parameters:
| Name | Type | Description |
|---|---|---|
date |
Date |
Returns:
- Type
- string
formatPrettyDate(date) → {string}static
#
Format a Date to a date string without the year (static variant)
Parameters:
| Name | Type | Description |
|---|---|---|
date |
Date |
Returns:
- Type
- string
formatRelativeTimeOrDate(date, [now]) → {string}static
#
Format a Date as relative time such as "40 seconds ago", "3 months ago", or "in 40 seconds". (static variant)
If the browser does not support Intl.RelativeTimeFormat (Safari 11.1, iOS Safari 11.3-11.4),
it uses DateFormatter.formatTimeAndDate() as fallback.
Parameters:
Returns:
- Type
- string
formatTime(date) → {string}static
#
Format a Date to a time string (static variant)
Parameters:
| Name | Type | Description |
|---|---|---|
date |
Date |
Returns:
- Type
- string
formatTimeAndDate(date) → {string}static
#
Format a Date to a time and date string (static variant)
Parameters:
| Name | Type | Description |
|---|---|---|
date |
Date |
Returns:
- Type
- string
formatTimeAndDateRange(date1, date2) → {string}static
#
Format a date range, showing time and date. (static variant)
TODO: Have MW provide localisation. This function depends on the browser for localisation. It does not properly respect the user's date format preferences, and has limited language support.
Parameters:
Returns:
- Type
- string
formatTimeRange(date1, date2) → {string}static
#
Format a date range, showing time only. (static variant)
TODO: Have MW provide localisation.
Parameters:
Returns:
- Type
- string
getShortZoneName(date) → {string}static
#
Get the short name of the time zone, e.g. "PST".
Parameters:
| Name | Type | Description |
|---|---|---|
date |
Date | The reference date |
Returns:
- Type
- string
getZoneOffsetMinutes(date) → {number}static
#
Get the time zone offset as a number of minutes, in the sign-preserving convention, e.g. "+01:00" becomes 60.
Parameters:
| Name | Type | Description |
|---|---|---|
date |
Date | The reference date for DST |
Returns:
- Type
- number
Get the time zone offset as a number of minutes, in the sign-preserving convention, e.g.