Saturdays only in .JS Datepicker

≡放荡痞女 提交于 2019-12-12 03:23:50

问题


I'm working with a pre-integrated .js datepicker and need to ensure only Saturdays are selectable.

My code in it's current form:

(function($){
    "use strict";

$.fn.gdlr_datepicker_range = function(){
    $(this).datepicker({
        minDate: 0,
        dateFormat : 'yy-mm-dd',
        numberOfMonths: [1, 2],
        beforeShowDay: function(date) {



            var date1 = $.datepicker.parseDate('yy-mm-dd', $("#gdlr-check-in").val());
            var date2 = $.datepicker.parseDate('yy-mm-dd', $("#gdlr-check-out").val());
                        return [date.getDay() === 6, date1 && ((date.getTime() == date1.getTime()) || (date2 && date >= date1 && date <= date2)) ? "dp-highlight" : ""];


        },
        onSelect: function(dateText, inst) {
            var date1 = $.datepicker.parseDate('yy-mm-dd', $("#gdlr-check-in").val());
            var date2 = $.datepicker.parseDate('yy-mm-dd', $("#gdlr-check-out").val());
            if (!date1 || date2) {
                $("#gdlr-check-in").val(dateText);
                $("#gdlr-check-out").val("");
            } else {
                $("#gdlr-check-out").val(dateText).trigger('change');
            }
        },
        closeText: objectL10n.closeText,
        currentText: objectL10n.currentText,
        monthNames: objectL10n.monthNames,
        monthNamesShort: objectL10n.monthNamesShort,
        dayNames: objectL10n.dayNames,
        dayNamesShort: objectL10n.dayNamesShort,
        dayNamesMin: objectL10n.dayNamesMin,
        firstDay: objectL10n.firstDay
    });

If somebody could point me in the right direction with a snippet, I would be extremely grateful!


回答1:


Quoting the docs on beforeShowDay param:

A function that takes a date as a parameter and must return an array with:

[0]: true/false indicating whether or not this date is selectable
[1]: a CSS class name to add to the date`s cell or "" for the default presentation
[2]: an optional popup tooltip for this date

It's easy to guess the solution: just change beforeShowDay function so that it checks the day of week of selected date when setting the first element of the returned array:

return [date.getDay() === 6, /* the rest remains the same */];

Demo.




回答2:


Did you try using the getDay()?

$(function() {
  $("#datepicker").datepicker({
    beforeShowDay: function(date) {
        if(date.getDay() == 6) {
            return [true];   
        } else {
            return [false];   
        }
    }
  });
});


来源:https://stackoverflow.com/questions/32849575/saturdays-only-in-js-datepicker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!