DataType Utility: DataType.Date.format()
strftime
syntax.
To format a JavaScript Date object into a string, simply call the format()
function of the DataType.Date
class. You may pass in optional format and locale values, but the default format
value is "%F"
and the default locale
value is "en-US"
:
YUI().use("datatype-date", function(Y) { var output = Y.DataType.Date.format(new Date()}); alert(output); });
YUI().use("datatype-date", function(Y) { var output = Y.DataType.Date.format(new Date()}); alert(output); });
The format
configuration property accepts strftime
tokens:
YUI().use("datatype-date", function(Y) { var output = Y.DataType.Date.format(new Date(), {format:"%F"}); alert(output); output = Y.DataType.Date.format(new Date(), {format:"%r"}); alert(output); output = Y.DataType.Date.format(new Date(), {format:"%Y-%m-%d %T %B, %A"}); alert(output); });
YUI().use("datatype-date", function(Y) { var output = Y.DataType.Date.format(new Date(), {format:"%F"}); alert(output); output = Y.DataType.Date.format(new Date(), {format:"%r"}); alert(output); output = Y.DataType.Date.format(new Date(), {format:"%Y-%m-%d %T %B, %A"}); alert(output); });
The following locales are built-in and available by default: "en"
, "en-AU"
, "en-GB"
, and "en-US"
.
YUI().use("datatype-date", function(Y) { var output = Y.DataType.Date.format(new Date(), {format:"%r"}); alert(output); output = Y.DataType.Date.format(new Date(), {format:"%r", locale:"en-GB"}); alert(output); });
YUI().use("datatype-date", function(Y) { var output = Y.DataType.Date.format(new Date(), {format:"%r"}); alert(output); output = Y.DataType.Date.format(new Date(), {format:"%r", locale:"en-GB"}); alert(output); });
Additional locales may be added for your implementation:
YUI().use("datatype-date", function(Y) { Y.DataType.Date.Locale['fr'] = Y.merge(Y.DataType.Date.Locale, { a: ['Dim', 'Lun', 'Mar', 'Mer', 'Jeu', 'Ven', 'Sam'], A: ['Dimanche', 'Lundi', 'Mardi', 'Mercredi', 'Jeudi', 'Vendredi', 'Samedi'], b: ['Jan', 'Fév', 'Mar', 'Avr', 'Mai', 'Jun', 'Jui', 'Aoû', 'Sep', 'Cct', 'Nov', 'Déc'], B: ['Janvier', 'Février', 'Mars', 'Avril', 'Mai', 'Juin', 'Juillet', 'Août', 'Septembre', 'Octobre', 'Novembre', 'Décembre'], c: '%a %d %b %Y %T %Z', p: ['', ''], P: ['', ''], x: '%d.%m.%Y', X: '%T' }); var output = Y.DataType.Date.format(new Date(), {format:"%c", locale:"fr"}); alert(output); });
YUI().use("datatype-date", function(Y) { Y.DataType.Date.Locale['fr'] = Y.merge(Y.DataType.Date.Locale, { a: ['Dim', 'Lun', 'Mar', 'Mer', 'Jeu', 'Ven', 'Sam'], A: ['Dimanche', 'Lundi', 'Mardi', 'Mercredi', 'Jeudi', 'Vendredi', 'Samedi'], b: ['Jan', 'Fév', 'Mar', 'Avr', 'Mai', 'Jun', 'Jui', 'Aoû', 'Sep', 'Cct', 'Nov', 'Déc'], B: ['Janvier', 'Février', 'Mars', 'Avril', 'Mai', 'Juin', 'Juillet', 'Août', 'Septembre', 'Octobre', 'Novembre', 'Décembre'], c: '%a %d %b %Y %T %Z', p: ['', ''], P: ['', ''], x: '%d.%m.%Y', X: '%T' }); var output = Y.DataType.Date.format(new Date(), {format:"%c", locale:"fr"}); alert(output); });
The default format and locale may by defined globally via the Y.config
configuration object:
YUI().use("datatype-date", function(Y) { Y.config.dateFormat = "%c"; Y.config.locale = "en-GB"; // Defaults to global format "%c" and locale "en-GB" var output = Y.DataType.Date.format(new Date()); alert(output); });
YUI().use("datatype-date", function(Y) { Y.config.dateFormat = "%c"; Y.config.locale = "en-GB"; // Defaults to global format "%c" and locale "en-GB" var output = Y.DataType.Date.format(new Date()); alert(output); });