使用变量在日期选取器中修改 maxDate 和 minDate



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

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 变量,则无法将其传递到对象中。

事实上,我无法真正弄清楚下面的整个代码是如何工作的。 我不确定格式化程序函数在做什么,也不清楚末尾的对象与整个函数的关系。 对于有点笼统的问题,我感到抱歉,但我发现日期选择器文档令人困惑。

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

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

最新更新