YUI 3.x Home -

Site Search (YDN & YUIBlog):

# YUI Library Examples: DataType Utility: DataType.Date.format()

## DataType Utility: DataType.Date.format()

The Date module of the DataType Utility allows you to format a JavaScript Date object into a string with `strftime` syntax.

View example in new window.

Format (optional): Locale (optional):

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);
    });

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);
    });

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);
    });

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);
    });

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);
    });

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); });

Copyright © 2009 Yahoo! Inc. All rights reserved.

Privacy Policy - Terms of Service - Copyright Policy - Job Openings