Vy Pham Vy Pham - 6 months ago 178
Javascript Question

How to get date, month, year in jquery ui datepicker?

I have a sample code:

<div id="calendar"></div>
$(document).ready(function() {
$('#calendar').datepicker({
dateFormat: 'yy-m-d',
inline: true,
onSelect: function(dateText, inst) {
var month = dateText.getUTCMonth();
var day = dateText.getUTCDate();
var year = dateText.getUTCFullYear();
alert(day+month+year);
}
});
});


=> when run code is error, how to get this (date, month, year) ?

Eli Eli
Answer

You can use method getDate():

$('#calendar').datepicker({
    dateFormat: 'yy-m-d',
    inline: true,
    onSelect: function(dateText, inst) { 
        var date = $(this).datepicker('getDate'),
            day  = date.getDate(),  
            month = date.getMonth() + 1,              
            year =  date.getFullYear();
        alert(day + '-' + month + '-' + year);
    }
});

FIDDLE