简体   繁体   中英

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

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. 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. If I declare a maxDate variable in the formatter function, or globally, I'm not able to pass it into the object.

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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