Mojo.Format

Namespace Detail

Mojo.Format has functions related to formatting dates, times and numbers.

Most of the API methods accept an optional options parameter which can be used to specify extended options for each method. Currently, five properties are recognized:

Not all APIs use all properties; see each API's documentation for details.

All APIs default to using the device's current locale, so it is only necessary to supply the countryCode when you desire formatting other than that of the device's current locale. Otherwise, it can be omitted.

Method Summary


Method Detail

Mojo.Format.formatChoice(value, choiceString, model)

Format a choice string according to the given value and model. The choice string is a sequence of choices separated by a vertical bar character. Each choice has a value to match, a hash character, followed by the string to use if the value matches. The string cannot contain a vertical bar. The strings may contain references to objects in the given model that are used to format that string using the Template object. The syntax for the value of "2>" means "greater than 2". Similarly, the syntax "2<" means "less than 2". If the value of the choice is empty, that means to use that choice as the default string.

Example choice string:

0#There are no files|1#There is one file|2<#There are #{num} files.|#There are some files. 

In the above example, if the value passed in with the first parameter is 0, then use the first string "There are no files". If the value passed in with the first parameter is 1, then use the second string "There is one file". If the value is 2 or more, use the last string, "There are #{num} files." If no other choices match, then the default string of "There are some files." is used instead.

The strings may contain references (such as #{num}) to objects in the given model that are used to format the final string.

When this function is called this way:

var files = 2185;
var model = { num: files };
print(Mojo.Format.formatChoice(files,
  "0#There are no files|1#There is one file|2<#There are #{num} files.", model);

The output is:

There are 2185 files. 

Parameters

Returns

A formatted string corresponding to the given value and formatted with the given model.


{String} Mojo.Format.formatCurrency(amount, options)

Converts a number representing an amount of currency into a string, using the proper locale-based format for currency.

Parameters

Returns

Formatted string


Mojo.Format.formatDate(date, options)

Formats the date object based on the options.

Parameters

Returns

The date, formatted as a string.


{String} Mojo.Format.formatNumber(number, options)

Converts a number into a string, using the proper locale-based format for numbers.

Parameters

Returns

Formatted string


{String} Mojo.Format.formatPercent(percent, options)

Converts a number into a percent string, using the locale-based format for percentages. The number is expected to already be a percentage, and will not be multiplied by 100.

Parameters

Returns

Formatted string


Mojo.Format.formatRelativeDate(date, options)

Formats the date object based on the options.

Parameters

Returns

The date, formatted as a string.


Mojo.Format.getCurrentTimeZone()

Returns the current timezone of the device.

Returns

String


Mojo.Format.getFirstDayOfWeek(options)

Returns a zero-based index into the week indicating what day is the first day of the country's or region's calendar week: Sunday: 0, Monday: 1, Tuesday: 2, etc. By default, returns the index appropriate for the device's current region setting.

Parameters

Returns

Number


Mojo.Format.isAmPmDefault(options)

Returns whether the current locale "normally" uses AM/PM as opposed to 24-hour time.

Parameters

Returns

Boolean


{string} Mojo.Format.runTextIndexer(text)

Searches the parameter text for URLs (web and mailto) and emoticons (if support is enabled) and returns a new string with those entities replaced by HTML links and images (respectively).

Parameters

Returns:

{string} HTML-ized version of input string


Mojo.Format.using12HrTime()

Returns whether the current app should use AM/PM as opposed to 24-hour time.

Returns

Boolean