2011-01-11 37 views
7

我正在使用jquery UI Datepicker插件,我试图从datepicker中选取不同变量中选定的日,月和年(最终解决方案是将所有3变量隐藏字段)。如何从jquery datepicker得到选定的日/月和年

下面是代码:

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

我所选择的日期 “1/10/2011” 和它返回 “1”。我选择了“1/25/2011”的日期,并返回“2”。我在这里做错了什么?

感谢您的帮助!

+0

什么格式jQuery是在datetext变量返回的日期?这可能会抛出Date对象。 – Fahad 2011-01-11 14:36:03

回答

19

getDay返回星期:)当天

您可以使用:

和getFullYear年度
GETDATE月份的一天
得到月的月份年份

按照获取者的完整列表(从DevDocs):

Date.prototype.getDate()根据当地时间返回指定日期的月份的日期(1-31)。
Date.prototype.getDay()根据当地时间返回指定日期的星期几(0-6)。
Date.prototype.getFullYear()根据当地时间返回指定日期的年份(4位数年份的4位数字)。
Date.prototype.getHours()根据当地时间返回指定日期的小时(0-23)。
Date.prototype.getMilliseconds()根据当地时间返回指定日期的毫秒数(0-999)。
Date.prototype.getMinutes()根据当地时间返回指定日期的分钟数(0-59)。
Date.prototype.getMonth()根据当地时间返回指定日期中的月份(0-11)。
Date.prototype.getSeconds()根据当地时间返回指定日期中的秒数(0-59)。
Date.prototype。getTime()返回指定日期的数值作为1970年1月1日以来的毫秒数,UTC为00:00:00(以前时间为负数)。
Date.prototype.getTimezoneOffset()返回时区以分钟偏移当前区域。
Date.prototype.getUTCDate()按照通用时间在指定的日期返回月份(1-31)的日子(日期)。
Date.prototype.getUTCDay()按照通用时间在指定的日期返回一周(0-6)的日子。
Date.prototype.getUTCFullYear()按照通用时间返回年份中的指定日期(4位数字为4位数的年份)。
Date.prototype.getUTCHours()按照通用时间返回指定日期的小时(0-23)。
Date.prototype.getUTCMilliseconds()根据通用时间返回指定日期的毫秒数(0-999)。
Date.prototype.getUTCMinutes()按照通用时间返回指定日期的分钟(0-59)。
Date.prototype.getUTCMonth()根据通用时间返回指定日期中的月份(0-11)。
Date.prototype.getUTCSeconds()根据通用时间返回指定日期中的秒数(0-59)。
Date.prototype.getYear()根据当地时间返回指定日期的年份(通常为2-3位数字)。改用getFullYear()**。

+0

每天学习新东西;)谢谢! – TheWebGuy 2011-01-11 14:50:30

3

其唯一返回.getDay()(0-6星期),你需要做的是这样的:

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

参考: http://www.w3schools.com/jsref/jsref_obj_date.asp

注意

要特别注意到什么返回,getDay() - > 0-6 getMonth() - > 0-11,等等...所有在我提供的参考。

0

从输入字段抢VAL:

var start_date=$('input#start_date').val(); 
相关问题