jQuery ui datepicker,从 onSelect 获取星期几
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5237478/
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
jQuery ui datepicker, get Day of week from onSelect
提问by SteveCl
Is it possible to get the day of the week from the onSelect event of the datepicker.
是否可以从日期选择器的 onSelect 事件中获取星期几。
I know I can get the day of the month, but I want the day of the week. Even just the day index will be fine e.g. 0-6.
我知道我可以得到一个月中的哪一天,但我想要一周中的哪一天。即使只是日指数也可以,例如 0-6。
I know you can get it in the beforeShowDay (or similar) event, but I need to react differently depending on the day of the week chosen. It's to do with setting the opening times.
我知道您可以在 beforeShowDay(或类似)活动中获得它,但我需要根据所选的星期几做出不同的反应。这与设置开放时间有关。
I could hit the server and find out, but that is not going to come back fast enough...ideally i want it all done on the client.
我可以访问服务器并找出答案,但这不会足够快地恢复……理想情况下,我希望这一切都在客户端上完成。
回答by Alnitak
This should do the trick:
这应该可以解决问题:
function(event, ui) {
var date = $(this).datepicker('getDate');
var dayOfWeek = date.getUTCDay();
};
This has the advantage of not needing to parse whatever arbitrary date format you've chosen for the Datepicker's text field - the getDate
method returns a native Javascript Date
object which you can then easily extract the date fields you need.
这样做的优点是不需要解析您为 Datepicker 的文本字段选择的任何任意日期格式 - 该getDate
方法返回一个本机 JavascriptDate
对象,然后您可以轻松地提取您需要的日期字段。
回答by Bradley Bossard
Also, if you want the actual name of the day of the week (without using the getUTCDay index into a array of something like ['Sunday', 'Monday', ...], you can use the datepicker object itself to give this to you, like below
此外,如果您想要一周中某天的实际名称(不使用 getUTCDay 索引到 ['Sunday', 'Monday', ...] 之类的数组中,您可以使用 datepicker 对象本身来给出这个给你,如下
var curDate = $(this).datepicker('getDate');
var dayName = $.datepicker.formatDate('DD', curDate);
This can also be used to get a shortened day of the week name (M), long or short day of the month (MM or M) by substituting DD for whatever component you're after.
这也可用于通过将 DD 替换为您所追求的任何组件来获得缩短的星期名称 (M)、月份的长或短(MM 或 M)。
回答by Avitus
I would onSelect convert the value of the textbox to be a javascript date and the use getUTCDay() to get the value of the day of the week. http://www.w3schools.com/jsref/jsref_getutcday.asp
我会 onSelect 将文本框的值转换为 javascript 日期,并使用 getUTCDay() 来获取星期几的值。 http://www.w3schools.com/jsref/jsref_getutcday.asp