# new DateFmt(options)
Parameters:
Name | Type | Description |
---|---|---|
options |
Object
|
options governing the way this date formatter instance works |
Methods
# format(dateLike) → {string}
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:
Name | Type | Description |
---|---|---|
dateLike |
IDate
|
number
|
string
|
Date
|
JulianDay
|
null
|
undefined
|
a date-like object to format |
the formatted version of the given date instance
string
# formatRelative(reference, date) → {string}
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:
- within a minute: either "X seconds ago" or "in X seconds"
- within an hour: either "X minutes ago" or "in X minutes"
- within a day: either "X hours ago" or "in X hours"
- within 2 weeks: either "X days ago" or "in X days"
- within 12 weeks (~3 months): either "X weeks ago" or "in X weeks"
- within two years: either "X months ago" or "in X months"
- longer than 2 years: "X years ago" or "in X years"
Parameters:
Name | Type | Description |
---|---|---|
reference |
IDate
|
number
|
string
|
Date
|
JulianDay
|
null
|
undefined
|
a date that the date parameter should be relative to |
date |
IDate
|
number
|
string
|
Date
|
JulianDay
|
null
|
undefined
|
a date being formatted |
"Wrong calendar type" when the start or end dates are not the same calendar type as the formatter itself
the formatted relative date
string
# getCalendar() → {string}
Return the name of the calendar used to format date/times for this formatter instance.
the name of the calendar used by this formatter
string
# getClock() → {string}
Return the clock option set in the constructor. If the clock option was not given, the default from the locale is returned instead.
"12" or "24" depending on whether this formatter uses the 12-hour or 24-hour clock
string
# getDateComponentOrder() → {string}
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.
a string giving the date component order
string
# getDateComponents() → {string}
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.
the date components that this formatter formats
string
# getDaysOfWeek(optionsopt) → {Array}
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:
- length - length of the names of the months being sought. This may be one of "short", "medium", "long", or "full"
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
options |
Object
|
<optional> |
an object-literal that contains one key "length" with the standard length strings |
an array of all of the names of the days of the week
Array
# getLength() → {string}
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.
the length of formats this formatter returns
string
# getLocale() → {Locale}
Return the locale used with this formatter instance.
the Locale instance for this formatter
# getMeridiemsRange() → {Array.<{name:string, start:string, end:string}>}
Return the meridiems range in current locale.
the range of available meridiems
Array.<{name:string, start:string, end:string}>
# getMonthsOfYear(optionsopt) → {Array}
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:
- length - length of the names of the months being sought. This may be one of "short", "medium", "long", or "full"
- date - retrieve the names of the months in the date of the given date
- year - retrieve the names of the months in the given year. In some calendars, the months have different names depending if that year is a leap year or not.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
options |
Object
|
<optional> |
an object-literal that contains any of the above properties |
an array of the names of all of the months of the year in the current calendar
Array
# getTemplate() → {string}
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.
the format template for this formatter
string
# getTimeComponents() → {string}
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.
the time components that this formatter formats
string
# getTimeZone() → {TimeZone}
Return the time zone used to format date/times for this formatter instance.
a time zone object that this formatter is formatting for
# getType() → {string}
Return the type of this formatter. The type is a string that has one of the following values: "time", "date", "datetime".
the type of the formatter
string
# toString() → {string}
Convert this formatter to a string representation by returning the format template. This method delegates to getTemplate.
the format template
string
# static getMeridiemsRange(options) → {Array.<{name:string, start:string, end:string}>}
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:
- locale - locale to use when formatting the date/time. If the locale is not specified, then the default locale of the app or web page will be used.
- meridiems - string that specifies what style of meridiems to use with this format. The choices are "default", "gregorian", "ethiopic", and "chinese". The "default" style is often the simple Gregorian AM/PM, but the actual style is chosen by the locale. (For almost all locales, the Gregorian AM/PM style is most frequently used.) The "ethiopic" style uses 5 different meridiems for "morning", "noon", "afternoon", "evening", and "night". The "chinese" style uses 7 different meridiems corresponding to the various parts of the day. N.B. Even for the Chinese locales, the default is "gregorian" when formatting dates in the Gregorian calendar.
Parameters:
Name | Type | Description |
---|---|---|
options |
Object
|
options governing the way this date formatter instance works for getting meridiems range |
Array.<{name:string, start:string, end:string}>
# static isIntlDateTimeAvailable(locale) → {Boolean}
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. If the locale is not specified, then it returns false.
Parameters:
Name | Type | Description |
---|---|---|
locale |
string
|
locale to check if it is available or not. |
true if it is available to use, false otherwise
Boolean