Fork me on GitHub

Example: Basic date field creation

How to create a date field.

Basic DateField creation

Use the following code to create a basic inputEx DateField.

Change date format

Set the formatDate to your localized date format ! Here is the french format (dd/mm/yyyy) :

Updated event and return value

The DateField returns a javascript Date object instance :

Use a string input/output formatting

The setValue/getValue methods will parse/format the dates to the valueFormat option.


<div id="field-container"></div>



YUI().use('inputex-date', function(Y) {

    // example 1
    new Y.inputEx.DateField({parentEl: 'container1', value: new Date(), showMsg: true});

    // example 2
    new Y.inputEx.DateField({dateFormat: 'd/m/Y', value: '27/03/2008', parentEl: 'container2', showMsg: true});

    // example 3
    var logDiv3 ='#container3');
    var dateField = new Y.inputEx.DateField({value: new Date(),  parentEl: logDiv3, showMsg: true, required:true, placeholder:'m/d/Y'}); 
    dateField.on('updated', function(val) {
        logDiv3.appendChild('div', null, null, val) );

    var button1 ='button', null, null, 'setValue()');
    logDiv3.appendChild(button1);'click', function() { dateField.setValue("11/26/1980"); });

    var button2 ='button', null, null, 'getValue()');
    logDiv3.appendChild(button2);'click', function() { alert(dateField.getValue()); });

    // example 4
    var stringdateField = new Y.inputEx.DateField({value: new Date(),  parentEl: 'container4', showMsg: true, required:true, placeholder:'m/d/Y', valueFormat: 'Y-m-d'});

    // Logger
    var logDiv4 ='#container4');
    stringdateField.on('updated', function(val) {
        logDiv4.appendChild('div', null, null, val) );

    // Set the value according to the valueFormat
    var button1 ='button', null, null, 'setValue()');
    logDiv4.appendChild(button1);'click', function() { stringdateField.setValue("1980-11-16"); });

    // The returned value will use the valueFormat too
    var button2 ='button', null, null, 'getValue()');
    logDiv4.appendChild(button2);'click', function() { alert(stringdateField.getValue()); });