namespace anychart.format Improve this Doc
The anychart.format namespace contains different static functions for format of texts, dates, numbers, etc.
Constants Overview
| locales | Contains a list of the used localizations. |
Functions Overview
| date() | Formats date by pattern. |
| dateTime() | Formats date time by the pattern. |
| getDateTimeFormat() | Gets date time format from locale by identifier and index. |
| getDateTimeFormats() | Gets all date time formats from locale by identifier. |
| getIntervalIdentifier() | Gets interval identifier by unit name. |
| getMessage() | Returns localized message if the translation is provided in messages section of output locale. |
| inputBaseDate() | Input base date settings. |
| inputDateTimeFormat() | Input date time format |
| inputLocale() | Input format settings to be used. |
| number() | Formats number. |
| outputDateFormat() | Output date format settings to be used. |
| outputDateTimeFormat() | Output date time format settings to be used. |
| outputLocale() | Output format settings to be used. |
| outputTimeFormat() | Output time format settings to be used. |
| outputTimezone() | Output timezone settings. |
| parseDateTime() | Parses input value to date. |
| parseNumber() | Parses a value to number according to locale set. |
| subs() | Formats for string substitution. |
| time() | Formats time by pattern. |
Typedefs Overview
| anychart.format.DateTimeLocale | Type definition for date time localization. |
| anychart.format.Locale | Type definition for localization. |
| anychart.format.NumberLocale | Type definition for number locale. |
Classes Overview
| anychart.format.Context | The Context class contains methods to show some information from extra fields in the format function context. |
Constants Description
Contains a list of the used localizations.
By default contains localization "en-en". See the full localizations list by link.
The field can contain a few localizations. See samples below.
Anychart.format.locales map accepts keys (localization names) or an object with settings from anychart.format.Locale.
| Keys | Object |
|---|---|
| "af-za" | {dateTimeLocale: { 'eras': ['v.C.', 'n.C.'], 'eraNames': ['voor Christus', 'na Christus'], } numberLocale: { 'decimalsCount': 3, 'decimalPoint': ':' } |
Type: Object.
Functions Description
date
The same that anychart.format.dateTime but with the anychart.format#outputDateFormat as default datetime format.
Params:
| Name | Type | Description |
|---|---|---|
| date | number | Date | UTC timestamp or Date object. |
| timeZone | number | Adjust with time zone. Indicate minutes WEST of UTC to be used as a constant time zone offset. |
| locale | string | anychart.format.Locale | Locale to be used. |
Returns:
string - Formatted value.Try it:
dateTime
Params:
| Name | Type | Description |
|---|---|---|
| date | number | Date | UTC timestamp or Date object. |
| format | string | Format of date ['yyyy.MM.dd']. |
| timeZone | number | Adjust with time zone. Indicate minutes WEST of UTC to be used as a constant time zone offset. |
| locale | string | anychart.format.Locale | Locale to be used. |
Returns:
string - Formatted value.Try it:
getDateTimeFormat
Params:
| Name | Type | Default | Description |
|---|---|---|---|
| identifier | string | Use anychart.format#getIntervalIdentifier to get correct identifier. | |
| index | number | 0 | If locale has more than one format. |
| locale | anychart.format.Locale | Locale to be used. |
Returns:
string -Use anychart.format.getDateTimeFormat
// for example in 'de-de' locale defined
// 'year_day': ['EEEE, y MMMM dd', 'yy MM dd', 'yyyy MMMM dd']
var identifier = anychart.format.getIntervalIdentifier('days','years');
var format = anychart.format.getDateTimeFormat(identifier, 2, 'de-de');
// format = 'yyyy MMMM dd'getDateTimeFormats
To get one format by index use anychart.format#getDateTimeFormat.
Params:
| Name | Type | Description |
|---|---|---|
| identifier | string | Use anychart.format#getIntervalIdentifier to get correct identifier |
| locale | anychart.format.Locale | Locale to be used. |
Returns:
string -Use anychart.format.getDateTimeFormat
// for example in 'de-de' locale defined
// 'year_day': ['EEEE, y MMMM dd', 'yy MM dd', 'yyyy MMMM dd']
var identifier = anychart.format.getIntervalIdentifier('days','years');
var allFormats = anychart.format.getDateTimeFormat(identifier, 'de-de');
// allFormats = ['EEEE, y MMMM dd', 'yy MM dd', 'yyyy MMMM dd']getIntervalIdentifier
Params:
| Name | Type | Description |
|---|---|---|
| intervalUnit | anychart.enums.Interval | string | Name of unit interval. |
| parentIntervalUnit | anychart.enums.Interval | string | Name of parent unit interval. |
Returns:
string - Correct interval identifier by unit name.Use anychart.format.getIntervalIdentifier
anychart.format.getIntervalIdentifier('day','year');
anychart.format.getIntervalIdentifier('d','y');
// returns 'year_day'getMessage
Params:
| Name | Type | Description |
|---|---|---|
| keyword | string | Keyword. |
Returns:
string - Message from current locale by keyword or a keyword itself if a message doesn't exist.Try it:
inputBaseDate
Returns:
Date - The input base date.Date -Defaults to the first millisecond of current UTC month.
Params:
| Name | Type | Description |
|---|---|---|
| date | Date | number | Date to set. |
Returns:
Date - The input base date.anychart.format.inputBaseDate(Date.UTC(2010, 10, 15, 16, 23, 10, 113));
// Try to parse
anychart.format.parseDateTime("13:31:16", "HH:mm:ss");
// Returns Date.UTC(2010, 10, 15, 13, 31, 16, 113)Try it:
inputDateTimeFormat
Returns:
string - The date time format.var dateTimeFormat = anychart.format.inputDateTimeFormat();
Params:
| Name | Type | Description |
|---|---|---|
| format | string | Input date time format for parsing |
Returns:
string - The date time format.Try it:
inputLocale
Returns:
string | anychart.format.Locale - The localization name or settings.var inputLocale = anychart.format.inputLocale();
Params:
| Name | Type | Default | Description |
|---|---|---|---|
| settings | string | anychart.format.Locale | 'default' | Localization name or settings. |
Returns:
string | anychart.format.Locale - The localization name or settings.Try it:
number
Params:
| Name | Type | Default | Description |
|---|---|---|---|
| number | number | Number for formatting. | |
| decimalsCountOrLocal | number | anychart.format.NumberLocale | string | 2 | Desired number of decimals or localization. |
| decimalPoint | string | "." | String to modify the decimal separator. |
| groupsSeparator | string | "" | String to separate every three digits. |
| scale | Object | boolean | false | Automatic scaling (divides by factor and substitutes suffix). If pass "true", will be used default scale. |
| zeroFillDecimals | boolean | false | Whether to fill in the remaining decimal places zeros. |
| scaleSuffixSeparator | string | "" | Additive between the suffix of scale and the number. Works only with scaling. |
| useBracketsForNegative | boolean | Whether to use brackets for negative value. |
Returns:
string - Formatted value.Try it:
Params:
| Name | Type | Description |
|---|---|---|
| number | number | Number for formatting. |
| settings | anychart.format.NumberLocale | Object with settings. |
Returns:
string - Formatted value.Try it:
outputDateFormat
NOTE: does not depend on anychart.format#outputDateTimeFormat.
Priority to return date format:
- Value from the setter
- Value from the 'dateFormat' field in the outputLocale
- Value from the 'dateFormat' field in the default locale
- 'yyyy.MM.dd'
Returns:
string - The output date format.Params:
| Name | Type | Description |
|---|---|---|
| format | string | Date format for parsing. |
Try it:
outputDateTimeFormat
Priority to return date format:
- Value from setter
- Value from field 'dateTimeFormat' in outputLocale
- Value from field 'dateTimeFormat' in default locale
- 'yyyy.MM.dd'
Returns:
string - The date time format.Params:
| Name | Type | Description |
|---|---|---|
| format | string | Date time format for parsing. |
Try it:
outputLocale
Returns:
string | anychart.format.Locale - The localization name or settings.var outputLocale = anychart.format.outputLocale();
Params:
| Name | Type | Description |
|---|---|---|
| settings | string | anychart.format.Locale | Localization name (from anychart.format#locales) or object of localization for output. |
Returns:
string | anychart.format.Locale - The localization name or settings.Try it:
outputTimeFormat
NOTE: independed from anychart.format#outputDateTimeFormat.
Priority to return date format:
- Value from setter
- Value from field 'timeFormat' in outputLocale
- Value from field 'timeFormat' in default locale
- 'HH:mm:ss'
Returns:
string - The current time format.Params:
| Name | Type | Description |
|---|---|---|
| format | string | Time format for parsing |
Try it:
outputTimezone
Returns:
number - The output offset.var outputTimezone = anychart.format.outputTimezone();
Adjusts time zone by value in minutes. Indicate minutes WEST of UTC to be used as the constant time zone offset.
Params:
| Name | Type | Default | Description |
|---|---|---|---|
| value | number | 0 | Value for adjusting time zone in minutes. |
Returns:
number - The output offset.Try it:
parseDateTime
Params:
| Name | Type | Description |
|---|---|---|
| value | * | Input value. |
| format | string | Format to be parsed. If undefined, anychart.format.inputDateTimeFormat is be used. |
| baseDate | Date | Date object to hold the parsed date. Used when input value doesn't contain
information about a year or a month or else. If parsing is successful this object contains absolutely the same values
of date time units as the return value. NOTE: If not Date, Date.UTC(currentYear, currentMoth) is be used. |
| locale | string | anychart.format.Locale | Locale to be used. If not set, anychart.format.inputLocale is be used. |
Returns:
Date - Parsed date or null if got wrong input value.Try it:
parseNumber
Params:
| Name | Type | Description |
|---|---|---|
| value | * | Value to be parsed. |
| locale | anychart.format.NumberLocale | string | Number locale to be used. If not defined, anychart.format.input.numberFormat will be used. |
Returns:
number - Parsed value. NaN if value could not be parsed.Try it:
subs
Params:
| Name | Type | Description |
|---|---|---|
| string | string | The string containing the pattern. |
| var_args | * | The items to substitute into the pattern. |
Returns:
string - A copy of {@code str} in which each occurrence of {@code %s} has been replaced an argument from {@code var_args}.Try it:
time
The same that anychart.format.dateTime but with the anychart.format#outputTimeFormat as default datetime format.
Params:
| Name | Type | Description |
|---|---|---|
| date | number | Date | UTC timestamp or Date object. |
| timeZone | number | Adjust with time zone. Indicate minutes WEST of UTC to be used as a constant time zone offset. |
| locale | string | anychart.format.Locale | Locale to be used. |
Returns:
string - Formatted value.Try it:
