Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 the datepicker in different variables (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?

Thanks for your help!

like image 481
TheWebGuy Avatar asked Jan 11 '11 14:01

TheWebGuy


2 Answers

getDay return the day of the week :)

You can use:

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

Follows a complete list of the getters (from DevDocs):

Date.prototype.getDate() Returns the day of the month (1-31) for the specified date according to local time.
Date.prototype.getDay() Returns the day of the week (0-6) for the specified date according to 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) in the specified date according to local time.
Date.prototype.getMilliseconds() Returns the milliseconds (0-999) in the specified date according to local time.
Date.prototype.getMinutes() Returns the minutes (0-59) in the specified date according to local time.
Date.prototype.getMonth() Returns the month (0-11) in the specified date according to local time.
Date.prototype.getSeconds() Returns the seconds (0-59) in 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 for prior times).
Date.prototype.getTimezoneOffset() Returns the time-zone offset in minutes for the current locale.
Date.prototype.getUTCDate() Returns the day (date) of the month (1-31) in the specified date according to universal time.
Date.prototype.getUTCDay() Returns the day of the week (0-6) in the specified date according to universal time.
Date.prototype.getUTCFullYear() Returns the year (4 digits for 4-digit years) in the specified date according to universal time.
Date.prototype.getUTCHours() Returns the hours (0-23) in the specified date according to universal time.
Date.prototype.getUTCMilliseconds() Returns the milliseconds (0-999) in the specified date according to universal time.
Date.prototype.getUTCMinutes() Returns the minutes (0-59) in the specified date according to universal time.
Date.prototype.getUTCMonth() Returns the month (0-11) in the specified date according to universal time.
Date.prototype.getUTCSeconds() Returns the seconds (0-59) in the specified date according to universal time.
Date.prototype.getYear() Returns the year (usually 2-3 digits) in the specified date according to local time. Use getFullYear()** instead.

like image 82
Ivan Buttinoni Avatar answered Oct 15 '22 22:10

Ivan Buttinoni


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

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

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

NOTE

Pay special attention to what is returned, getDay() -> 0-6 getMonth() -> 0-11, etc... all in the reference I provided.

like image 32
Jakub Avatar answered Oct 15 '22 20:10

Jakub