Search code examples
javascriptdatepickermootools

Getting value of MooTools datepicker


I am using this MooTools datepicker.

How can I get the current value (either Unix TS or date) from it with JavaScript?

My code is:

window.addEvent('load', function() {
    new DatePicker('.demo_vista', {
        pickerClass: 'datepicker_vista',
        format: 'm/d/Y',
        allowEmpty: true,
        toggleElements: '.date_toggler'
    });
});

I have tried the standard getElementById('mydatepicker').value but I get [object NodeList] everytime.


Solution

  • There are different versions of this datepicker. Some output UNIX some not.

    To get the value from the input field/datepicker, If you have a id for it you can use $('mydatepicker').value or document.id('mydatepicker').value, or plain javascript like you described. Check if the value you get out $('mydatepicker').value is already UNIX. otherwise you can parse the date. 

    If you don't have UNIX timestamp from the input value you can convert the date to it by using:

    var unix = new Date(this.value).format('%s');
    

    In the options (if you don't get a UNIX timestamp) you can also add inputOutputFormat: 'U', to specify UNIX should be the value of the input field (some versions have this as default). This input field is hidden because datepicker creates a clone that is the one you see. But this hidden field is the one in the html markup and so the one with ID.

    Check this demo, change a date and look at console.