【问题标题】:How make default current date in selectable in jquery date picker如何在 jquery 日期选择器中选择默认当前日期
【发布时间】:2017-05-27 10:42:09
【问题描述】:

在这里我正在禁用除当前星期之外的日期。它工作正常也有点在这里我面临一个问题

这里我想做默认的当前日期是可选择的,怎么做?

$(function () {
$(".bhj").datepicker({
    //In Datepicker set the Calendar display start with Monday
    firstDay: 1,
    //Before Populating the Calendar set the Enabled & Disabled Dates using beforeShowDay(Date) function
    beforeShowDay: function (date) {
        //var monday = new Date("June 1, 2013 00:00:00");
        //Get today's date
        var monday = new Date();
        //Set the time of today's date to 00:00:00 
        monday.setHours(0,0,0,0);
        //alert(monday.getDay() + ' : ' + monday.getDate() + ' : ' + (monday.getDay() || 7) + ' : ' + monday);
        /*
        Below Line sets the Date to Monday (Start of that Week)
        (monday.getDay() || 7) returns the value of getDay() 
        ie., [ 1 - Mon, 2 - Tue, 3 - Wed, 4 - Thu, 5 - Fri, 6 - Sat ]  
        except for Sunday where it returns 7. 
        The return value is used to calculate the Date of that Week's Monday
        */
        monday.setDate(monday.getDate() + 1 - (monday.getDay() || 7));
        //Set the Date to Monday
        var sunday = new Date(monday);
        //Now add 6 to Monday to get the Date of Sunday (End of that Week)
        sunday.setDate(monday.getDate() + 5);
        //Now return the enabled and disabled dates to datepicker
        return [(date >= monday && date <= sunday), ''];
    }
});
//Set the date format to dd/mm/yy ie., 30/10/1989
$(".bhj").datepicker("option", "dateFormat", "dd/mm/yy");
});

【问题讨论】:

标签: jquery datepicker jquery-ui-datepicker


【解决方案1】:
$( ".bhj" ).datepicker({dateFormat:"yy/mm/dd"}).datepicker("setDate",new Date());

你可以试试这个。

【讨论】:

    【解决方案2】:

    您的问题很难理解,但我认为您的意思是您希望日期选择器在今天默认?仅供参考,there is documentation,请查看所有示例。

    $(".bhj").datepicker();
    $(".bhj").datepicker("setDate", new Date());
    

    或者,如果您希望从今天开始选择它(所以不要及时返回):

    $(".bhj").datepicker();
    $(".bhj").datepicker("minDate ", 0); // or -1 for yesterday, or +1 for tomorrow etc
    

    或者,如果您只想要工作日 (see this answer):

    $( ".datepicker.future").datepicker('option','beforeShowDay',function(date){
        var td = date.getDay();
        var ret = [(date.getDay() != 0 && date.getDay() != 6),'',(td != 'Sat' && td != 'Sun')?'':'only on workday'];
        return ret;
    });
    

    【讨论】:

      猜你喜欢
      • 2017-08-11
      • 2011-10-02
      • 2011-04-19
      • 1970-01-01
      • 2021-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-12
      相关资源
      最近更新 更多