The DataType Utility is a collection of classes that provide type-conversion and string-formatting convenience methods for numbers, dates, and XML documents.
Y.Date.format()
will output dates as strings formatted
using strftime
tokens. Some of the formats are localizable, so
be sure to specify the lang
property of the YUI instance's
config object.
The module has support for a large number of languages built in. If you need a language that's not supported, you can register the necessary localized data yourself using facilities of the Internationalization utility. The resource bundle you provide needs to have properties corresponding to the locale-sensitive strftime format specifiers:
Y.Date.parse()
accepts a value and, optionally, a formatting
specification and converts it to a Date
object. If no format is given, it will pass the value to the
JavaScript Date()
constructor. Invalid values will return null.
The `parser` function can accept the same format specification as `Y.Date.format`. Ideally, using the same formatting specification, the two operations should be complementary. The `parser` method, however is more tolerant, it accepts any number of spaces in between the parts of the date, is case-insensitive and does not require leading zeros even when the format says so.
The `parser` function accepts an optional third argument, a `cutoff` year. When years are given as two digits, those values below the cutoff year will have 2000 added to them, those above, 1900. If `cutoff` is passed as null, the date will be taken as is. The `cutoff` defaults to 30.
``` var date1 = Y.Date.parse("00-1-1", "%F"); // Assumes Jan 1st, 2000 var date2 = Y.Date.parse("40-1-1", "%F"); // Assumes Jan 1st, 1940 var date3 = Y.Date.parse("140-1-1", "%F"); // No guessing: Jan 1st, 140 var date4 = Y.Date.parse("40-1-1", "%F", null); // No guessing: Jan 1st, 40 ```For the purpose of the calculation, `parser` ignores the parts that do not provide sufficient information on their own such as the names of the day of the week. However, they are required to be present in the input string.
The `%Z` format or the composite formats that use it, such as `%c` in the `en` locale have limited support. It relies on a table of time zone names and abbreviations which would be too heavy to fully load. The module has the table filled with a minimal set of North American and European abbreviations.
It is suggested that the developer uses different formatting specifications when simply displaying a date and when editing it. For displaying purposes, the format can add more information (such as the weekday) and extra decoration. For editing purposes, the format should be simpler so as not to force the user to type so much, and it should be easier and unambiguous to the parser.
JavaScript Number values can be formatted with a variety options into
string values using Y.Number.format()
.
String values can be converted into Number objects with
Y.Number.parse()
.
An optional configuration can be added, compatible with that of `Y.Number.format`. If found, any prefix, suffix, thousands separators and whitespaces will be deleted, the decimal separator will be replaced by a dot and the resulting string parsed. The `decimalPlaces` property is ignored and is meant for compatibility with `format`.
``` Y.log(Y.Number.parse(" € 123.123.123,176 (EUR) ",{ prefix: "€", thousandsSeparator: ".", decimalSeparator: ",", decimalPlaces: 2, suffix: " (EUR)" })); ```
Y.XML.format()
will accept an XML document and return
its string representation. Note that browsers may slightly differ in the
exact string that is returned.
Y.XML.parse()
will accept a string representation of
XML and return an XML document object. Note that browsers differ in their
handling of invalid syntax but will in general return an XML document even
under error conditions.
The functions Y.Date.parse()
and
Y.Number.parse()
are registered with the
Parsers
object for seamless integration with the DataSchema
Utility. For dynamic type enforcing when data is being normalized against a
schema, simply point to the appropriate function using the built-in
shortcut in your schema definition. Parsing your data in this manner is
essential if your numerical or date data comes over the wire as JSON, since
all the values will be strings.