jQuery ui datepicker,从 onSelect 获取星期几 [英] jQuery ui datepicker, get Day of week from onSelect
问题描述
是否可以从日期选择器的 onSelect 事件中获取星期几.
Is it possible to get the day of the week from the onSelect event of the datepicker.
我知道我可以得到一个月中的哪一天,但我想要一周中的哪一天.即使只是日指数也可以,例如0-6.
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.
我知道您可以在 beforeShowDay(或类似)活动中获得它,但我需要根据所选的星期几做出不同的反应.与设置开放时间有关.
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.
我可以访问服务器并找出结果,但这不会足够快地恢复...理想情况下,我希望这一切都在客户端上完成.
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.
推荐答案
这应该可以解决问题:
function(event, ui) {
var date = $(this).datepicker('getDate');
var dayOfWeek = date.getUTCDay();
};
这样做的优点是不需要解析您为 Datepicker 的文本字段选择的任意日期格式 - getDate
方法返回一个本机 Javascript Date
对象,该对象然后,您可以轻松提取所需的日期字段.
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.
这篇关于jQuery ui datepicker,从 onSelect 获取星期几的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!