Expand all

mediawiki.DateFormatter

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
Source:

Returns:

Type
string
Format a Date to a date string (non-static)

formatDateRange(date1, date2) → {string} #

Format a date range, showing date only. (non-static)

TODO: Have MW provide localisation.

Parameters:

Name Type Description
date1 Date
date2 Date
Source:

Returns:

Type
string
Format a date range, showing date only.

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
Source:

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
Source:

Returns:

Type
string
Format a Date in ISO 8601 format, including timezone designator.

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
Source:

Returns:

Type
string
Format a Date to a MediaWiki 14-character timestamp.

formatPrettyDate(date) → {string} #

Format a Date to a date string without the year (non-static)

Parameters:

Name Type Description
date Date
Source:

Returns:

Type
string
Format a Date to a date string without the year (non-static)

formatTime(date) → {string} #

Format a Date to a time string (non-static)

Parameters:

Name Type Description
date Date
Source:

Returns:

Type
string
Format a Date to a time string (non-static)

formatTimeAndDate(date) → {string} #

Format a Date to a time and date string (non-static)

Parameters:

Name Type Description
date Date
Source:

Returns:

Type
string
Format a Date to a time and date string (non-static)

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:

Name Type Description
date1 Date
date2 Date
Source:

Returns:

Type
string
Format a date range, showing time and date.

formatTimeRange(date1, date2) → {string} #

Format a date range, showing time only. (non-static)

TODO: Have MW provide localisation.

Parameters:

Name Type Description
date1 Date
date2 Date
Source:

Returns:

Type
string
Format a date range, showing time only.

getShortZoneName(date) → {string} #

Get the short name of the time zone, e.g. "PST".

Parameters:

Name Type Description
date Date

The reference date

Source:

Returns:

Type
string
Get the short name of the time zone, e.g.

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

Source:

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.

Source:

Returns:

Type
module:mediawiki.DateFormatter

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

Source:

Returns:

Type
module:mediawiki.DateFormatter
Get a DateFormatter instance configured for the current user

forUtc() → {module:mediawiki.DateFormatter}static #

Get a DateFormatter instance, configured for the current user except with the time zone set to UTC.

Source:

Returns:

Type
module:mediawiki.DateFormatter

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
Source:

Returns:

Type
string
Format a Date to a date string (static variant)

formatDateRange(date1, date2) → {string}static #

Format a date range, showing date only. (static variant)

TODO: Have MW provide localisation.

Parameters:

Name Type Description
date1 Date
date2 Date
Source:

Returns:

Type
string
Format a date range, showing date only.

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
Source:

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
Source:

Returns:

Type
string
Format a Date in ISO 8601 format, including timezone designator.

formatPrettyDate(date) → {string}static #

Format a Date to a date string without the year (static variant)

Parameters:

Name Type Description
date Date
Source:

Returns:

Type
string
Format a Date to a date string without the year (static variant)

formatTime(date) → {string}static #

Format a Date to a time string (static variant)

Parameters:

Name Type Description
date Date
Source:

Returns:

Type
string
Format a Date to a time string (static variant)

formatTimeAndDate(date) → {string}static #

Format a Date to a time and date string (static variant)

Parameters:

Name Type Description
date Date
Source:

Returns:

Type
string
Format a Date to a time and date string (static variant)

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:

Name Type Description
date1 Date
date2 Date
Source:

Returns:

Type
string
Format a date range, showing time and date.

formatTimeRange(date1, date2) → {string}static #

Format a date range, showing time only. (static variant)

TODO: Have MW provide localisation.

Parameters:

Name Type Description
date1 Date
date2 Date
Source:

Returns:

Type
string
Format a date range, showing time only.

getShortZoneName(date) → {string}static #

Get the short name of the time zone, e.g. "PST".

Parameters:

Name Type Description
date Date

The reference date

Source:

Returns:

Type
string
Get the short name of the time zone, e.g.

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

Source:

Returns:

Type
number

Get the time zone offset as a number of minutes, in the sign-preserving convention, e.g.