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.

HTML

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

JavaScript

Implementation

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 = Y.one('#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( Y.inputEx.cn('div', null, null, val) );
    });

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

    var button2 = Y.inputEx.cn('button', null, null, 'getValue()');
    logDiv3.appendChild(button2);
    Y.one(button2).on('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 = Y.one('#container4');
    stringdateField.on('updated', function(val) {
        logDiv4.appendChild( Y.inputEx.cn('div', null, null, val) );
    });

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

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

});