jQuery ui datepicker,从 onSelect 获取星期几
是否可以从日期选择器的 onSelect 事件中获取星期几。
我知道我可以获得该月的某一天,但我想要星期几。即使只是日期索引也可以,例如 0-6。
我知道您可以在 beforeShowDay (或类似)事件中获得它,但我需要根据所选的星期几做出不同的反应。 这与设置开放时间有关。
我可以访问服务器并找出答案,但这不会足够快地返回......理想情况下,我希望这一切都在客户端上完成。
Is it possible to get the day of the week from the onSelect event of the datepicker.
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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这应该可以解决问题:
这样做的优点是不需要解析您为日期选择器的文本字段选择的任意日期格式 -
getDate
方法返回本机 JavascriptDate
对象,然后您可以轻松提取所需的日期字段。This should do the trick:
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 JavascriptDate
object which you can then easily extract the date fields you need.另外,如果您想要一周中某一天的实际名称(而不使用 getUTCDay 索引到类似 ['Sunday', 'Monday', ...] 的数组中,您可以使用 datepicker 对象本身来给出这个给你,就像下面这样,
通过用 DD 替换你想要的任何组成部分,它也可以用于获取一周中的缩短日名称 (M)、一个月中的长日或短日 (MM 或 M)。
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
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.
我会将 onSelect 将文本框的值转换为 JavaScript 日期,并使用 getUTCDay() 获取星期几的值。
http://www.w3schools.com/jsref/jsref_getutcday.asp
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