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

时间:2019-06-11 02:19:49

标签: javascript jquery object datepicker

我使用此处找到的基本模板构造了一个日期选择器:

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

我的代码是:

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.

});

虽然一切正常,但我想将maxDate设置为从今天起一周。虽然我知道我可以做类似的事情:

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

我不知道如何将变量传递到maxDate:对象中。如果我在格式化程序函数中或全局声明了maxDate变量,则无法将其传递给对象。

实际上,我什至无法弄清楚下面的整个代码是如何工作的。我不确定格式化程序的功能在做什么,也不清楚最后的对象如何与整个函数相关。抱歉,这个问题有些笼统,但我发现datepicker文档令人困惑。

1 个答案:

答案 0 :(得分:2)

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

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