简体   繁体   English

使用变量在datepicker中修改maxDate和minDate

[英]Modifying maxDate and minDate in datepicker using variables

I've constructed a datepicker using the basic template found here: 我使用这里找到的基本模板构造了一个日期选择器:

https://github.com/qodesmith/datepicker/blob/master/README.md https://github.com/qodesmith/datepicker/blob/master/README.md

My code is: 我的代码是:

const picker = datepicker(document.querySelector('#datepicker'), {



 // Event callbacks.
    onSelect: function(instance) {

    var instanceSplit = instance.dateSelected.toString().split(" " ,4)
    var instanceClean = instanceSplit.toString().replace(/,/g, ' ')
    selectedDate = instanceClean
    console.log(selectedDate)
    update()

    },


     onShow: function(instance) {
        console.log('Calendar showing.');
      },
      onHide: function(instance) {
        console.log('Calendar hidden.');
      },
      onMonthChange: function(instance) {
        // Show the month of the selected date.
      },



      // Customizations.
      formatter: function(el, date, instance) {
        // This will display the date as `1/1/2019`.
        el.value = date.toDateString();
      },

      position: 'tr', // Top right.
      startDay: 1, // Calendar week starts on a Monday.
      customDays: ['S', 'M', 'T', 'W', 'Th', 'F', 'S'],
      customMonths: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
      overlayButton: 'Go!',
      overlayPlaceholder: 'Enter a 4-digit year',

      // Settings.
      alwaysShow: true, // Never hide the calendar.
      dateSelected: new Date(), // Today is selected.
      maxDate: new Date(2019, 5, 21), // Jan 1st, 2099.
      minDate: new Date(2016, 5, 1), // June 1st, 2016.
      startDate: new Date(), // This month.

});

While it all works, I'd like to set the maxDate to be a week from today. 尽管一切正常,但我想将maxDate设置为从今天起一周。 While I know I could do something like: 虽然我知道我可以做类似的事情:

    var firstDay = new Date();
    var nextWeek = new Date(firstDay.getTime() + 7 * 24 * 60 * 60 * 1000);

I can't figure out how to pass a variable into maxDate: object. 我不知道如何将变量传递给maxDate:对象。 If I declare a maxDate variable in the formatter function, or globally, I'm not able to pass it into the object. 如果我在格式化程序函数中或全局声明了maxDate变量,则无法将其传递给对象。

In fact, I can't really figure out how the entire code below is even working. 实际上,我什至无法弄清楚下面的整个代码是如何工作的。 I'm not sure what the formatter function is doing, nor am I clear on how the objects at the end relate to the function as a whole. 我不确定格式化程序的功能在做什么,也不清楚最后的对象如何与整个函数相关。 Apologies for the somewhat general question, but I'm finding the datepicker documentation confusing. 抱歉,这个问题有些笼统,但我发现datepicker文档令人困惑。

您可以尝试内联+7天计算。

maxDate: new Date(new Date().getTime() + 7 * 24 * 60 * 60 * 1000),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM