Vy Pham
Vy Pham

Reputation: 245

How to get date, month, year in jQuery UI datepicker?

I have this 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 I run the code, there is an error. How to get this (date, month, year)?

Upvotes: 24

Views: 119097

Answers (5)

Ben
Ben

Reputation: 2023

$("#date").datepicker('getDate').getMonth() + 1; 

The month on the datepicker is 0 based (0-11), so add 1 to get the month as it appears in the date.

Upvotes: 0

mix-fGt
mix-fGt

Reputation: 487

what about that simple way)

$(document).ready ->
 $('#datepicker').datepicker( dateFormat: 'yy-mm-dd',  onSelect: (dateStr) ->
    alert dateStr # yy-mm-dd
    #OR
    alert $("#datepicker").val(); # yy-mm-dd

Upvotes: 0

viclim
viclim

Reputation: 959

Use the javascript Date object.

$(document).ready(function()  {
    $('#calendar').datepicker({
      dateFormat: 'yy-m-d',
      inline: true,
      onSelect: function(dateText, inst) { 
            var date = new Date(dateText);
            // change date.GetDay() to date.GetDate()
            alert(date.getDate() + date.getMonth() + date.getFullYear());
     } 
    });
});

Upvotes: 5

KevinIsNowOnline
KevinIsNowOnline

Reputation: 773

Hi you can try viewing this jsFiddle.

I used this code:

var day = $(this).datepicker('getDate').getDate();  
var month = $(this).datepicker('getDate').getMonth();  
var year = $(this).datepicker('getDate').getYear();  

I hope this helps.

Upvotes: 12

Eli
Eli

Reputation: 14827

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

Upvotes: 50

Related Questions