v5.20192.624
|

Globalize Class

Class that implements formatting and parsing of numbers and Dates.

By default, Globalize uses the American English culture. To switch cultures, include the appropriate wijmo.culture.*.js file after the wijmo files.

The example below shows how you can use the Globalize class to format dates, times, and numbers in different cultures:

Example

Hierarchy

  • Globalize

Methods

Static format

  • format(value: any, format: string, trim?: boolean, truncate?: boolean): string
  • Formats a number or a date.

    The format strings used with the format function are similar to the ones used by Globalize.js and by the .NET Globalization library. The tables below contains links that describe the formats available:

    Parameters

    • value: any

      Number or Date to format (all other types are converted to strings).

    • format: string

      Format string to use when formatting numbers or dates.

    • Optional trim: boolean

      Whether to remove trailing zeros from numeric results.

    • Optional truncate: boolean

      Whether to truncate the numeric values rather than round them.

    Returns string

    A string representation of the given value.

Static formatDate

  • formatDate(value: Date, format: string): string
  • Formats a date using the current culture.

    The format parameter contains a .NET-style Date format string with the following additions:

    • Q, q Calendar quarter.
    • U Fiscal quarter (government).
    • u Fiscal quarter (private sector).
    • EEEE, EEE, EE, E Fiscal year (government).
    • eeee, eee, ee, e Fiscal year (private sector).

    For example:

    var d = new Date(2015, 9, 1); // Oct 1, 2015
    console.log(wijmo.Globalize.format(d, '"FY"EEEE"Q"U') + ' (US culture)');
    > FY2016Q1 (US culture)
    

    Another addition is available for dealing with complex eras such as those defined in the Japanese culture:

    • ggg Era name (e.g. '平成', '昭和', '大正', or '明治').
    • gg Era initial (e.g. '平', '昭', '大', or '明').
    • g Era symbol (e.g. 'H', 'S', 'T', or 'M').

    Example

    Parameters

    • value: Date

      Number or Date to format.

    • format: string

      .NET-style Date format string.

    Returns string

    A string representation of the given date.

Static formatNumber

  • formatNumber(value: number, format: string, trim?: boolean, truncate?: boolean): string
  • Formats a number using the current culture.

    The formatNumber method accepts most .NET-style Standard Numeric Format Strings, except for the 'e' and 'x' formats (scientific notation and hexadecimal) which are not supported.

    Numeric format strings take the form Axxsscc, where:

    • A is a single case-insensitive alphabetic character called the format specifier.
    • xx is an optional integer called the precision specifier. The precision specifier affects the number of digits in the result.
    • ss is an optional string used to scale the number. If provided, it must consist of commas. The number is divided by 1000 for each comma specified.
    • cc is an optional string used to override the currency symbol when formatting currency values. This is useful when formatting currency values for cultures different than the current default (for example, when formatting Euro or Yen values in applications that use the English culture).

    The following table describes the standard numeric format specifiers and displays sample output produced by each format specifier for the default culture.

    n Number: formatNumber(1234.5, 'n2') => '1,234.50'
    f Fixed-point: formatNumber(1234.5, 'f2') => '1234.50'
    g General (no trailing zeros): formatNumber(1234.5, 'g2') => '1234.5'
    d Decimal (integers): formatNumber(-1234, 'd6') => '-001234'
    x Hexadecimal (integers): formatNumber(1234, 'x6') => '0004d2'
    c Currency: formatNumber(1234, 'c') => '$ 1,234.00'
    p Percent: formatNumber(0.1234, 'p2') => '12.34 %'

    The scaling specifier is especially useful when charting large values. For example, the markup below creates a chart that plots population versus GDP. The raw data expresses the population is units and the GDP in millions. The scaling specified in the axes formats causes the chart to show population in millions and GDP in trillions:

    <wj-flex-chart
      items-source="countriesGDP" binding-x="pop" chart-type="Scatter">
      <wj-flex-chart-series
        name="GDP" binding="gdp"></wj-flex-chart-series>
      <wj-flex-chart-axis
        wj-property="axisX" title="Population (millions)"
        format="n0,,">
      </wj-flex-chart-axis>
      <wj-flex-chart-axis
        wj-property="axisY" title="GDP (US$ trillions)"
        format="c0,,">
      </wj-flex-chart-axis>
    </wj-flex-chart>

    Parameters

    • value: number

      Number to format.

    • format: string

      .NET-style standard numeric format string (e.g. 'n2', 'c4', 'p0', 'g2', 'd2').

    • Optional trim: boolean

      Whether to remove trailing zeros from the result.

    • Optional truncate: boolean

      Whether to truncate the value rather than round it.

    Returns string

    A string representation of the given number.

Static getFirstDayOfWeek

  • getFirstDayOfWeek(): number
  • Gets the first day of the week according to the current culture.

    The value returned is between zero (Sunday) and six (Saturday).

    Returns number

Static getNumberDecimalSeparator

  • getNumberDecimalSeparator(): string
  • Gets the symbol used as a decimal separator in numbers.

    Returns string

Static parseDate

  • parseDate(value: string, format: string, defaultDate?: Date): Date
  • Parses a string into a Date.

    Two-digit years are converted to full years based on the value of the calendar's twoDigitYearMax property. By default, this is set to 2029, meaning two-digit values of 30 to 99 are parsed as 19, and values from zero to 29 are parsed as 20.

    You can change this threshold by assigning a new value to the calendar. For example:

    // get calendar
    var cal = wijmo.culture.Globalize.calendar;
    
    // default threshold is 2029, so "30" is parsed as 1930
    cal.twoDigitYearMax = 2029;
    var d1 = wijmo.Globalize.parseDate('30/12', 'yy/MM'); // dec 1930
    
    // changing threshold to 2100, so all values are parsed as 20**
    cal.twoDigitYearMax = 2100;
    var d2 = wijmo.Globalize.parseDate('30/12', 'yy/MM'); // dec 2030

    Parameters

    • value: string

      String to convert to a Date.

    • format: string

      Format string used to parse the date.

    • Optional defaultDate: Date

      Date to use as a reference in case date parts are missing form the input (e.g. when format = 'MM/dd').

    Returns Date

    The date represented by the given string, or null if the string cannot be parsed into a Date.

Static parseFloat

  • parseFloat(value: string, format?: string): number
  • Parses a string into a floating point number.

    Parameters

    • value: string

      String to convert to a number.

    • Optional format: string

      Format to use when parsing the number.

    Returns number

    The floating point number represented by the given string, or NaN if the string cannot be parsed into a floating point number.

Static parseInt

  • parseInt(value: string, format?: string): number
  • Parses a string into an integer.

    Parameters

    • value: string

      String to convert to an integer.

    • Optional format: string

      Format to use when parsing the number.

    Returns number

    The integer represented by the given string, or NaN if the string cannot be parsed into an integer.