开发者

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

开发者 https://www.devze.com 2023-02-03 10:24 出处:网络
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

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(
  开发者_Go百科          {
                 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!


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.


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.


To grab val from input field:

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号