AnyChart
API Reference
Still have questions?
Contact support
Top

namespace anychart.format Improve this Doc

The anychart.format namespace contains different static functions for format of texts, dates, numbers, etc.

Constants Overview

localesContains 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.DateTimeLocaleType definition for date time localization.
anychart.format.LocaleType definition for localization.
anychart.format.NumberLocaleType definition for number locale.

Classes Overview

anychart.format.ContextThe 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.anychart.format.Locale>

Functions Description

date

Formats date by pattern.
The same that anychart.format.dateTime but with the anychart.format#outputDateFormat as default datetime format.

Params:

NameTypeDescription
datenumber | DateUTC timestamp or Date object.
timeZonenumberAdjust with time zone. Indicate minutes WEST of UTC to be used as a constant time zone offset.
localestring | anychart.format.LocaleLocale to be used.

Returns:

string - Formatted value.

dateTime

Formats date time by the pattern.

Params:

NameTypeDescription
datenumber | DateUTC timestamp or Date object.
formatstringFormat of date ['yyyy.MM.dd'].
timeZonenumberAdjust with time zone. Indicate minutes WEST of UTC to be used as a constant time zone offset.
localestring | anychart.format.LocaleLocale to be used.

Returns:

string - Formatted value.

getDateTimeFormat

Gets date time format from locale by identifier and index. To get all formats use anychart.format#getDateTimeFormats.

Params:

NameTypeDefaultDescription
identifierstring
Use anychart.format#getIntervalIdentifier to get correct identifier.
indexnumber
0
If locale has more than one format.
localeanychart.format.Locale
Locale to be used.

Returns:

string -
See listing
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

Gets all date time formats from locale by identifier.
To get one format by index use anychart.format#getDateTimeFormat.

Params:

NameTypeDescription
identifierstringUse anychart.format#getIntervalIdentifier to get correct identifier
localeanychart.format.LocaleLocale to be used.

Returns:

string -
See listing
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

Gets interval identifier by unit name.

Params:

NameTypeDescription
intervalUnitanychart.enums.Interval | stringName of unit interval.
parentIntervalUnitanychart.enums.Interval | stringName of parent unit interval.

Returns:

string - Correct interval identifier by unit name.
See listing
Use anychart.format.getIntervalIdentifier
anychart.format.getIntervalIdentifier('day','year');
anychart.format.getIntervalIdentifier('d','y');
// returns 'year_day'

getMessage

Returns localized message if the translation is provided in messages section of output locale.

Params:

NameTypeDescription
keywordstringKeyword.

Returns:

string - Message from current locale by keyword or a keyword itself if a message doesn't exist.

inputBaseDate

Getter for the input base date.

Returns:

Date - The input base date.Date -
Setter for the input base date.

Params:

NameTypeDescription
dateDate | numberDate to set.

Returns:

Date - The input base date.
See listing
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)

inputDateTimeFormat

Getter for the input date time format.

Returns:

string - The date time format.
See listing
var dateTimeFormat = anychart.format.inputDateTimeFormat();
Setter for the input date time format. Has priority over the format of localization.

Params:

NameTypeDescription
formatstringInput date time format for parsing

Returns:

string - The date time format.

inputLocale

Getter for input format settings.

Returns:

string | anychart.format.Locale - The localization name or settings.
See listing
var inputLocale = anychart.format.inputLocale();
Setter for input format settings.

Params:

NameTypeDefaultDescription
settingsstring | anychart.format.Locale
'default'
Localization name or settings.

Returns:

string | anychart.format.Locale - The localization name or settings.

number

Formats a number with given settings.

Params:

NameTypeDefaultDescription
numbernumber
Number for formatting.
decimalsCountOrLocalnumber | anychart.format.NumberLocale | string
2
Desired number of decimals or localization.
decimalPointstring
"."
String to modify the decimal separator.
groupsSeparatorstring
""
String to separate every three digits.
scaleObject | boolean
false
Automatic scaling (divides by factor and substitutes suffix). If pass "true", will be used default scale.
zeroFillDecimalsboolean
false
Whether to fill in the remaining decimal places zeros.
scaleSuffixSeparatorstring
""
Additive between the suffix of scale and the number. Works only with scaling.
useBracketsForNegativeboolean
Whether to use brackets for negative value.

Returns:

string - Formatted value.
Formats a number using object.

Params:

NameTypeDescription
numbernumberNumber for formatting.
settingsanychart.format.NumberLocaleObject with settings.

Returns:

string - Formatted value.

outputDateFormat

Getter for the only current output date format.

Returns:

string - The output date format.
Setter for the only output date format.

Params:

NameTypeDescription
formatstringDate format for parsing.

outputDateTimeFormat

Getter for the output date and time format.

Returns:

string - The date time format.
Output date and time format. Has priority over the format of localization.

Params:

NameTypeDescription
formatstringDate time format for parsing.

outputLocale

Getter for output format settings.

Returns:

string | anychart.format.Locale - The localization name or settings.
See listing
var outputLocale = anychart.format.outputLocale();
Setter for output format settings.

Params:

NameTypeDescription
settingsstring | anychart.format.LocaleLocalization name (from anychart.format#locales) or object of localization for output.

Returns:

string | anychart.format.Locale - The localization name or settings.

outputTimeFormat

Getter for the only current output time format.

Returns:

string - The current time format.
Setter for the only output time format.

Params:

NameTypeDescription
formatstringTime format for parsing

outputTimezone

Getter for the output offset.

Returns:

number - The output offset.
See listing
var outputTimezone = anychart.format.outputTimezone();
Setter for the output offset.
Adjusts time zone by value in minutes. Indicate minutes WEST of UTC to be used as the constant time zone offset.

Params:

NameTypeDefaultDescription
valuenumber
0
Value for adjusting time zone in minutes.

Returns:

number - The output offset.

parseDateTime

Parses input value to date.

Params:

NameTypeDescription
value*Input value.
formatstringFormat to be parsed. If undefined, anychart.format.inputDateTimeFormat is be used.
baseDateDateDate 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.
localestring | anychart.format.LocaleLocale to be used. If not set, anychart.format.inputLocale is be used.

Returns:

Date - Parsed date or null if got wrong input value.

parseNumber

Parses a value to number according to locale set.

Params:

NameTypeDescription
value*Value to be parsed.
localeanychart.format.NumberLocale | stringNumber 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.

subs

Formats for string substitution. For example: subs("foo%s hot%s", "bar", "dog") becomes "foobar hotdog".

Params:

NameTypeDescription
stringstringThe 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}.

time

Formats time by pattern.
The same that anychart.format.dateTime but with the anychart.format#outputTimeFormat as default datetime format.

Params:

NameTypeDescription
datenumber | DateUTC timestamp or Date object.
timeZonenumberAdjust with time zone. Indicate minutes WEST of UTC to be used as a constant time zone offset.
localestring | anychart.format.LocaleLocale to be used.

Returns:

string - Formatted value.