Class Index | File Index

Classes


Class DateFmt

Create a new date formatter instance. The date formatter is immutable once it is created, but can format as many different dates as needed with the same options. Create different date formatter instances for different purposes and then keep them cached for use later if you have more than one date to format.

The options may contain any of the following properties:

Any substring containing letters within single or double quotes will be used as-is in the final output and will not be interpretted for codes as above.

Example: a date format in Spanish might be given as: "'El' d. 'de' MMMM", where the 'El' and the 'de' are left as-is in the output because they are quoted. Typical output for this example template might be, "El 5. de Mayo". The following options will be used when formatting a date/time with an explicit template:

All other options will be ignored and their corresponding getter methods will return the empty string.


Defined in: DateFmt.js.

Class Summary
Constructor Attributes Constructor Name and Description
 
DateFmt(options)
Method Summary
Method Attributes Method Name and Description
 
format(dateLike)
Format a particular date instance according to the settings of this formatter object.
 
formatRelative(reference, date)
Return a string that describes a date relative to the given reference date.
 
Return the name of the calendar used to format date/times for this formatter instance.
 
Return the clock option set in the constructor.
 
Return the order of the year, month, and date components for the current locale.
 
Return the date components that this formatter formats.
 
getDaysOfWeek(options)
Returns an array of the days of the week, formatted to the optional length specified.
 
Return the length used to format date/times in this formatter.
 
Return the locale used with this formatter instance.
<static>  
DateFmt.getMeridiemsRange(options)
Return the range of possible meridiems (times of day like "AM" or "PM") in this date formatter.
 
Return the meridiems range in current locale.
 
getMonthsOfYear(options)
Returns an array of the months of the year, formatted to the optional length specified.
 
Return the template string that is used to format date/times for this formatter instance.
 
Return the time components that this formatter formats.
 
Return the time zone used to format date/times for this formatter instance.
 
Return the type of this formatter.
<static>  
DateFmt.isIntlDateTimeAvailable(locale)
return true if the locale is supported in date and time formatting for Intl.DateTimeFormat Object
  • locale - locale to check if it is available or not.
 
Convert this formatter to a string representation by returning the format template.
Class Detail
DateFmt(options)
Parameters:
{Object} options
options governing the way this date formatter instance works
Method Detail
{string} format(dateLike)
Format a particular date instance according to the settings of this formatter object. The type of the date instance being formatted must correspond exactly to the calendar type with which this formatter was constructed. If the types are not compatible, this formatter will produce bogus results.
Parameters:
{IDate|number|string|Date|JulianDay|null|undefined} dateLike
a date-like object to format
Returns:
{string} the formatted version of the given date instance

{string} formatRelative(reference, date)
Return a string that describes a date relative to the given reference date. The string returned is text that for the locale that was specified when the formatter instance was constructed.

The date can be in the future relative to the reference date or in the past, and the formatter will generate the appropriate string.

The text used to describe the relative reference depends on the length of time between the date and the reference. If the time was in the past, it will use the "ago" phrase, and in the future, it will use the "in" phrase. Examples:

Parameters:
{IDate|number|string|Date|JulianDay|null|undefined} reference
a date that the date parameter should be relative to
{IDate|number|string|Date|JulianDay|null|undefined} date
a date being formatted
Throws:
"Wrong calendar type" when the start or end dates are not the same calendar type as the formatter itself
Returns:
{string} the formatted relative date

{string} getCalendar()
Return the name of the calendar used to format date/times for this formatter instance.
Returns:
{string} the name of the calendar used by this formatter

{string} getClock()
Return the clock option set in the constructor. If the clock option was not given, the default from the locale is returned instead.
Returns:
{string} "12" or "24" depending on whether this formatter uses the 12-hour or 24-hour clock

{string} getDateComponentOrder()
Return the order of the year, month, and date components for the current locale.

When implementing a date input widget in a UI, it would be useful to know what order to put the year, month, and date input fields so that it conforms to the user expectations for the locale. This method gives that order by returning a string that has a single "y", "m", and "d" character in it in the correct order.

For example, the return value "ymd" means that this locale formats the year first, the month second, and the date third, and "mdy" means that the month is first, the date is second, and the year is third. Four of the 6 possible permutations of the three letters have at least one locale that uses that ordering, though some combinations are far more likely than others. The ones that are not used by any locales are "dym" and "myd", though new locales are still being added to CLDR frequently, and possible orderings cannot be predicted. Your code should support all 6 possibilities, just in case.

Returns:
{string} a string giving the date component order

{string} getDateComponents()
Return the date components that this formatter formats. This is either the value of the date option to the constructor, or the default value. If this formatter is a time-only formatter, this method will return the empty string. The date component letters may be specified in any order in the constructor, but this method will reorder the given components to a standard order.
Returns:
{string} the date components that this formatter formats

{Array} getDaysOfWeek(options)
Returns an array of the days of the week, formatted to the optional length specified. i.e. ...getDaysOfWeek() OR ...getDaysOfWeek({length: "short"})

The options parameter may contain any of the following properties:

Parameters:
{Object=} options
an object-literal that contains one key "length" with the standard length strings
Returns:
{Array} an array of all of the names of the days of the week

{string} getLength()
Return the length used to format date/times in this formatter. This is either the value of the length option to the constructor, or the default value.
Returns:
{string} the length of formats this formatter returns

{Locale} getLocale()
Return the locale used with this formatter instance.
Returns:
{Locale} the Locale instance for this formatter

<static> {Array.<{name:string|start:string|end:string}>} DateFmt.getMeridiemsRange(options)
Return the range of possible meridiems (times of day like "AM" or "PM") in this date formatter.

The options may contain any of the following properties:

Parameters:
{Object} options
options governing the way this date formatter instance works for getting meridiems range
Returns:
{Array.<{name:string|start:string|end:string}>}

{Array.<{name:string|start:string|end:string}>} getMeridiemsRange()
Return the meridiems range in current locale.
Returns:
{Array.<{name:string|start:string|end:string}>} the range of available meridiems

{Array} getMonthsOfYear(options)
Returns an array of the months of the year, formatted to the optional length specified. i.e. ...getMonthsOfYear() OR ...getMonthsOfYear({length: "short"})

The options parameter may contain any of the following properties:

Parameters:
{Object=} options
an object-literal that contains any of the above properties
Returns:
{Array} an array of the names of all of the months of the year in the current calendar

{string} getTemplate()
Return the template string that is used to format date/times for this formatter instance. This will work, even when the template property is not explicitly given in the options to the constructor. Without the template option, the constructor will build the appropriate template according to the options and use that template in the format method.
Returns:
{string} the format template for this formatter

{string} getTimeComponents()
Return the time components that this formatter formats. This is either the value of the time option to the constructor, or the default value. If this formatter is a date-only formatter, this method will return the empty string. The time component letters may be specified in any order in the constructor, but this method will reorder the given components to a standard order.
Returns:
{string} the time components that this formatter formats

{TimeZone} getTimeZone()
Return the time zone used to format date/times for this formatter instance.
Returns:
{TimeZone} a time zone object that this formatter is formatting for

{string} getType()
Return the type of this formatter. The type is a string that has one of the following values: "time", "date", "datetime".
Returns:
{string} the type of the formatter

<static> {Boolean} DateFmt.isIntlDateTimeAvailable(locale)
return true if the locale is supported in date and time formatting for Intl.DateTimeFormat Object
Parameters:
{string} locale
locale to check if it is available or not.
Returns:
{Boolean} true if it is available to use, false otherwise

{string} toString()
Convert this formatter to a string representation by returning the format template. This method delegates to getTemplate.
Returns:
{string} the format template

Documentation generated by JsDoc Toolkit 2.4.0 on Tue Jan 17 2023 15:56:28 GMT-0800 (Pacific Standard Time)