jQuery 如何从jquery datepicker获取选定的日/月和年
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4658641/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get selected day/month and year from jquery datepicker
提问by TheWebGuy
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).
我正在使用 Jquery UI Datepicker 插件,我试图从不同变量中的 datepicker 获取选定的日、月和年(最终解决方案是将所有 3 个变量分配给隐藏字段)。
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?
我选择了日期“1/10/2011”,它返回“1”。我选择了日期“1/25/2011”,它返回“2”。我在这里做错了什么?
Thanks for your help!
谢谢你的帮助!
回答by Ivan Buttinoni
getDay return the day of the week :)
getDay 返回星期几:)
You can use:
您可以使用:
getFullYearfor the year
getDatethe day of the month
getMonththe month of the year
getFullYear年份
getDate月份中的第几天
getMonth年份中的月份
Follows a complete list of the getters (from DevDocs):
遵循 getter 的完整列表(来自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.
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)。
日期.prototype.getMonth()根据当地时间返回指定日期的月份(0-11)。
Date.prototype.getSeconds()根据当地时间返回指定日期的秒数(0-59)。
Date.prototype.getTime()返回指定日期的数值,表示自 1970 年 1 月 1 日 00:00:00 UTC 以来的毫秒数(先前时间为负)。
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)。
日期.prototype.getYear()根据当地时间返回指定日期的年份(通常为 2-3 位)。请改用 getFullYear()**。
回答by Jakub
Its only returning .getDay()
(day of the week 0-6), you need to do something like:
它唯一的返回.getDay()
(一周中的第 0-6 天),您需要执行以下操作:
var selDay = ...
var selMon = startDate.getMonth();
var selYear = startDate.getYear();
Reference: http://www.w3schools.com/jsref/jsref_obj_date.asp
参考: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.
请特别注意返回的内容,getDay() -> 0-6 getMonth() -> 0-11 等...都在我提供的参考中。
回答by Diablo
To grab val from input field:
从输入字段中获取 val:
var start_date=$('input#start_date').val();