javascript jQuery:日期选择器,提醒所选日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6480742/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 20:49:50  来源:igfitidea点击:

jQuery: datepicker, alert the dayname of the selected

javascriptjqueryjquery-uidatepickerjquery-ui-datepicker

提问by Karem

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

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

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

因此,当您选择 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>

回答by Francois Deschenes

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

jQueryUI 的 Datepicker 带有一个formatDate可以为您完成此操作的函数。如果您使用的是本地化版本,它也会以该语言显示日期。

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/.

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

回答by citizen conn

<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>

回答by WooDzu

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)

如果您不介意在输入框中显示日期名称,这将起作用,如果您不喜欢它,您可以使用第二个隐藏输入(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/

示例在http://jsfiddle.net/aa74R/

回答by bhargav

Use inbuilt function

使用内置函数

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

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

}});

回答by pimvdb

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

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

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

and use the .getDayfunction. You could do this:

并使用该.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)