Format Number And Date

CDF has a default library to format numbers i.e. cdo.NumberFormat and other to format dates i.e. moment

Q: How can I format numbers in CDF?

A:

In CDF we provide the function Dashboards.numberFormat to format numbers with a given mask using the dashboard default language or optionally with a specific language.
The mask syntax is mostly compatible with VB's format function mask syntax, only the number related subset is relevant.
Other elements of this mask syntax are 'C' or 'Currency' and 'A' or 'Abbreviation' to insert the currency symbol and abbreviate the number.

Examples:

1. Format a number using the dashboard language, e.g. 'en-us':
    Dashboards.numberFormat(1200, '0,#.0')                -> "1,200.0"
    Dashboards.numberFormat(1200, '0,0.#')                -> "1,200"
    Dashboards.numberFormat(1200.46, '0,0.#')             -> "1,200.5"

    Dashboards.numberFormat(5480, '0.00 Abbreviation')    -> "5,49 k"
    Dashboards.numberFormat(5480000, '0.00A')             -> "5,49m"
    Dashboards.numberFormat(5480000000, '0.00A')          -> "5,49b"
    Dashboards.numberFormat(5480000000000, '0.00A')       -> "5,49t"

    Dashboards.numberFormat(4500.64, '0,0.0\u00a4')       -> "4,500.6$"
    Dashboards.numberFormat(4500000.64, '0,0.0 Currency') -> "4,500,000.6 $"
    Dashboards.numberFormat(4500.64, 'C 0,0.0')           -> "$ 4,500.6"

    Dashboards.numberFormat(0.2, '0.00%')                 -> "20.00%"
    Dashboards.numberFormat(1200, '0.0e00')               -> "1.2e03"
    Dashboards.numberFormat(4500000, '0.0AC')             -> "4.5m$"
            
2. Format a number using a specific language, will use fallback logic if the specified language isn't found:
    Dashboards.numberFormat(4500.64, '0.0,0 C', 'pt-pt') -> "4.500,6 €"
    Dashboards.numberFormat(4500.64, 'C 0,0.0', 'en-gb') -> "£ 4,500.6"
    Dashboards.numberFormat(4500.64, 'C 0,0.0', 'xx-zz') -> "$ 4,500.6" *Fallback to the default 'en-us'*
            

Q: How can I format dates in CDF?

A:

Similar to number format, here we provide the function Dashboards.dateFormat to format a date with a given mask.
For a detailed list of examples on how to use moment click here.
The mask syntax is explained in detail here.
To add more languages to moment you can do two things:

Examples:

1. Format a date using the dashboard language, e.g. 'en-us':
      Dashboards.dateFormat(moment(), 'DD/MM/YY')                                            -> "18/02/15"
      Dashboards.dateFormat(new Date(), 'dddd Do MMMM YYYY')                                 -> "Wednesday 18th February 2015"
      Dashboards.dateFormat(Dashboards.dateParse('13-08-1983', 'DD-MM-YYYY'), 'D MMM, YYYY') -> "13 Aug, 1983"
      Dashboards.dateFormat(null, 'dddd Do MMMM YYYY')                                       -> Invalid Date
              
2. Format a date using a specific language, will use fallback logic if the specified language isn't found:
      Dashboards.dateFormat(new Date(), 'dddd Do MMMM YYYY', 'en-us') -> "Wednesday 18th February 2015"
      Dashboards.dateFormat(new Date(), 'dddd Do MMMM YYYY', 'pt-pt') -> "quarta-feira 18º fevereiro 2015"
      Dashboards.dateFormat(new Date(), 'dddd Do MMMM YYYY', 'xx-zz') -> "Wednesday 18th February 2015" *Fallback to the default 'en-us'*
              

Q: How can I parse dates in CDF?

A:

As in the previous section, here we provide the function Dashboards.dateParse to parse a date with a given mask/format.
This function will always return a Date object containing the parsed date.

Examples:

      Dashboards.dateParse("Wednesday, February 18, 2015 12:00 AM", 'LLLL') -> Wed Feb 18 2015 00:00:00 GMT+0000 (WET)
      Dashboards.dateParse('13-08-1983', 'DD-MM-YYYY')                      -> Sat Aug 13 1983 00:00:00 GMT+0100 (WEST)
      Dashboards.dateParse(null, 'dddd Do MMMM YYYY')                       -> Invalid Date
              

Q: How can I configure new or existing languages?

A:

To configure a new or existing language you can use the function Dashboards.configLanguage by specifying the language code and a configuration object with the keywords:

Examples:

1. Configure the number's format language:
    Dashboards.configLanguage('foo-bar', {
        number: {
            mask:              '#,0.##',
            style: {
                integerPad:    '0',
                fractionPad:   '0',
                decimal:       ',',
                group:         ' ',
                groupSizes:    [3],
                abbreviations: ['k','m', 'b', 't'],
                negativeSign:  '-',
                currency:      'F'
            }
        }
    })
            
2. Configure the date's format language:
    Dashboards.configLanguage('foo-bar', {
        dateLocale: {
            months: [
                "January", "February", "March", "April", "May", "June",
                "July", "August", "September", "October", "November", "December"
            ],
            monthsShort: [
                "Jan", "Feb", "Mar", "Apr", "May", "Jun",
                "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
            ]
        }
    })
            
3. Configure both format language:
    Dashboards.configLanguage('foo-bar', {
        number: {
            mask:              '#,0.##',
            style: {
                integerPad:    '0',
                fractionPad:   '0',
                decimal:       ',',
                group:         ' ',
                groupSizes:    [3],
                abbreviations: ['k','m', 'b', 't'],
                negativeSign:  '-',
                currency:      'F'
            }
        },
        dateLocale: {
            months: [
                "January", "February", "March", "April", "May", "June",
                "July", "August", "September", "October", "November", "December"
            ],
            monthsShort: [
                "Jan", "Feb", "Mar", "Apr", "May", "Jun",
                "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
            ]
        }
    })