jQuery:日期选择器,提醒所选日期的名称

发布于 2024-11-16 23:00:41 字数 364 浏览 4 评论 0原文

我如何提醒所选日期的名称?例如“星期一”。

因此,当您选择 2011 年 6 月 7 日时,它会提醒“星期二”

<script>
$(function() {
    $( "#date" ).datepicker({ 
     dateFormat: 'dd/mm/yy',
     onSelect: function(dateText, inst) {
    // how can i grab the day name of the day, example "Monday" and alert it out?
    // alert( ? );
     }
    });
});
</script>

How can i alert the selected days name? Example 'Monday'.

So when you pick 7th june 2011 it will alert "Tuesday"

<script>
$(function() {
    $( "#date" ).datepicker({ 
     dateFormat: 'dd/mm/yy',
     onSelect: function(dateText, inst) {
    // how can i grab the day name of the day, example "Monday" and alert it out?
    // alert( ? );
     }
    });
});
</script>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

天气好吗我好吗 2024-11-23 23:00:41

jQueryUI 的 Datepicker 附带了一个 formatDate 函数,可以做到这一点为你。如果您使用本地化版本,它也会以该语言显示日期。

onSelect: function(dateText, inst) {
  var date = $(this).datepicker('getDate');
  alert($.datepicker.formatDate('DD', date));
}

有关 Dapicker 实用函数本地化的更多信息,请查看 http://jqueryui.com/demos/datepicker /

The jQueryUI's Datepicker comes with a formatDate function that can do that for you. If you're using a localized version, it'll show the days in that language too.

onSelect: function(dateText, inst) {
  var date = $(this).datepicker('getDate');
  alert($.datepicker.formatDate('DD', date));
}

For more information on localization of on Dapicker's utility functions, have a look at http://jqueryui.com/demos/datepicker/.

长发绾君心 2024-11-23 23:00:41
<script>
$(function() {
    $( "#date" ).datepicker({ 
         dateFormat: 'dd/mm/yy',
         onSelect: function(dateText, inst) {
         var weekday=new Array(7);
         weekday[0]="Sunday";
         weekday[1]="Monday";
         weekday[2]="Tuesday";
         weekday[3]="Wednesday";
         weekday[4]="Thursday";
         weekday[5]="Friday";
         weekday[6]="Saturday";

         alert(weekday[inst.getDate().getDay()];
     }
    });
});
</script>
<script>
$(function() {
    $( "#date" ).datepicker({ 
         dateFormat: 'dd/mm/yy',
         onSelect: function(dateText, inst) {
         var weekday=new Array(7);
         weekday[0]="Sunday";
         weekday[1]="Monday";
         weekday[2]="Tuesday";
         weekday[3]="Wednesday";
         weekday[4]="Thursday";
         weekday[5]="Friday";
         weekday[6]="Saturday";

         alert(weekday[inst.getDate().getDay()];
     }
    });
});
</script>
清欢 2024-11-23 23:00:41

如果您不介意在输入框中显示当天的名称,这将起作用
如果您不喜欢它,您可以使用第二个隐藏输入(http://jqueryui.com/demos/ datepicker/#alt-field)

$(function() {
    $( "#date" ).datepicker({
     dateFormat: 'DD, d MM, yy',
     onSelect: function(dateText, inst) {
         var stop = dateText.indexOf(',');
        alert( dateText.substring(0, stop));
     }
    });
});

示例位于 http://jsfiddle.net/aa74R/

this will work if you don't mind showing the name of the day in the input box
if you dont like it you can use second hidden input (http://jqueryui.com/demos/datepicker/#alt-field)

$(function() {
    $( "#date" ).datepicker({
     dateFormat: 'DD, d MM, yy',
     onSelect: function(dateText, inst) {
         var stop = dateText.indexOf(',');
        alert( dateText.substring(0, stop));
     }
    });
});

example at http://jsfiddle.net/aa74R/

乖不如嘢 2024-11-23 23:00:41

您可以像这样解析日期 (mm/dd/yyyy):

new Date(Date.parse("06/07/2011"))

并使用 .getDay 函数。你可以这样做:

// parse data
var regexp = /(\d{2})\/(\d{2})\/(\d{2})/.exec("07/06/11");

//form date
var date = new Date(Date.parse(regexp[2] + "/" + regexp[1] + "/20" + regexp[3]));

alert(date.getDay()); // 2 -> Tuesday (starts at 0 = Sunday)

You can parse a date like this (mm/dd/yyyy):

new Date(Date.parse("06/07/2011"))

and use the .getDay function. You could do this:

// parse data
var regexp = /(\d{2})\/(\d{2})\/(\d{2})/.exec("07/06/11");

//form date
var date = new Date(Date.parse(regexp[2] + "/" + regexp[1] + "/20" + regexp[3]));

alert(date.getDay()); // 2 -> Tuesday (starts at 0 = Sunday)
蓝咒 2024-11-23 23:00:41

使用内置函数

$("#datepicker" ).datepicker({  onSelect: function(dateText, inst) {

alert($.datepicker._defaults.dayNames[new Date(dateText).getDay()]);

}});

Use inbuilt function

$("#datepicker" ).datepicker({  onSelect: function(dateText, inst) {

alert($.datepicker._defaults.dayNames[new Date(dateText).getDay()]);

}});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文