How to get selected day / month and year from jquery datepicker

I am using the jQuery UI Datepicker plugin, I am trying to get the selected day, month and year from datepicker in different variables (the final solution is to assign all 3 variables to hidden fields).

Here is the code:

$(function() { $("#_StartDate").datepicker( { onSelect: function(dateText, inst) { var startDate = new Date(dateText); var selDay = startDate.getDay(); alert(selDay); } } ); }); 

I selected the date "1/10/2011" and it returns "1". I selected the date “1/25/2011” and it returns “2”. What am I doing wrong here?

Thank you for your help!

+7
source share
3 answers

getDay returns the day of the week :)

You can use:

getFullYear for the year
getDate day of the month
getMonth month of the year

Performs a complete list of getters (from DevDocs ):

Date.prototype.getDate () Returns the day of the month (1-31) on the specified date in accordance with local time.
Date.prototype.getDay () Returns the day of the week (0-6) on the specified date in accordance with local time.
Date.prototype.getFullYear () Returns the year (4 digits for 4-digit years) of the specified date according to local time.
Date.prototype.getHours () Returns the hour (0-23) on the specified date in accordance with local time.
Date.prototype.getMilliseconds () Returns milliseconds (0-999) on the specified date in accordance with local time.
Date.prototype.getMinutes () Returns minutes (0-59) on the specified date according to local time.
Date.prototype.getMonth () Returns the month (0-11) on the specified date according to local time.
Date.prototype.getSeconds () Returns seconds (0-59) on the specified date according to local time.
Date.prototype.getTime () Returns the numeric value of the specified date as the number of milliseconds since January 1, 1970, 00:00:00 UTC (negative value for previous times).
Date.prototype.getTimezoneOffset () Returns the timezone offset in minutes for the current locale.
Date.prototype.getUTCDate () Returns the day (date) of the month (1-31) on the specified date in accordance with universal time.
Date.prototype.getUTCDay () Returns the day of the week (0-6) on the given date according to universal time.
Date.prototype.getUTCFullYear () Returns the year (4 digits for 4-digit years) on the given date according to universal time.
Date.prototype.getUTCHours () Returns the clock (0-23) on the given date according to universal time.
Date.prototype.getUTCMilliseconds () Returns milliseconds (0-999) on the given date according to universal time.
Date.prototype.getUTCMinutes () Returns minutes (0-59) on the specified date according to universal time.
Date.prototype.getUTCMonth () Returns the month (0-11) on the given date according to universal time.
Date.prototype.getUTCSeconds () Returns seconds (0-59) on the given date according to universal time.
Date.prototype.getYear () Returns the year (usually 2-3 digits) on the specified date according to local time. Use getFullYear () ** instead.

+19
source

The only return is .getDay() (day of the week 0-6), you need to do something like:

 var selDay = ... var selMon = startDate.getMonth(); var selYear = startDate.getYear(); 

Link: http://www.w3schools.com/jsref/jsref_obj_date.asp

Note

Pay particular attention to what is returned, getDay () → 0-6 getMonth () → 0-11, etc. everything in the link that I provided.

+3
source

To capture val from an input field:

 var start_date=$('input#start_date').val(); 
0
source

All Articles