Editor - set a date range relative to today

Editor - set a date range relative to today

tbrcrltbrcrl Posts: 13Questions: 6Answers: 0

Hello, in the following a sample of my code

fields: [
...
{
    "label": "DATE:",
    "name": "date",
    "type": "datetime",
    "format": "ddd, D MMM YY",
    opts: {
      disableDays: [ 0, 6 ],
      minDate: new Date('2017-01-18'),
      maxDate: new Date('2017-01-23')
    }
},

I would like to have "today" as minDate and ("today" +4 working days) as maxDate, taking also into account that Saturday and Sunday have been disabled (this means that if "today" is Thursday, ("today" +4 working days) should be Wednesday.
Any suggestion?
Many thanks in advance

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 64,925Questions: 1Answers: 10,753 Site admin

    Basically do as you have done using new Date() but add 4 days to it:

    var date = new Date();
    date.setDate( date.getDate()+4 );
    

    Allan

  • tbrcrltbrcrl Posts: 13Questions: 6Answers: 0

    Thank, Allan, it works fine!
    Any suggestion how to simply exclude the 'disableDays' from the counting?

  • allanallan Posts: 64,925Questions: 1Answers: 10,753 Site admin

    No - I'm afraid that would need to be taken into the calculation. I suspect Moment will have a way of adding only working days.

    Allan

  • tbrcrltbrcrl Posts: 13Questions: 6Answers: 0

    Hi, I solved using pure JavaScript in this way

            var startDate = new Date();
            var cntrDate = new Date();
    
            var endDate = "", noOfDaysToAdd = 4, count = 0;
            while(count < noOfDaysToAdd){
              endDate = new Date(cntrDate.setDate(cntrDate.getDate() + 1));
              if(endDate.getDay() != 0 && endDate.getDay() != 6){
                count++;
              }
            }
    
            startDate.setDate( startDate.getDate()-1 );
    
            var editor = new $.fn.dataTable.Editor( {
            ...
             fields: [
             ...
             {
                  "label": "DATE:",
                  "name": "date",
                  "type": "datetime",
                  "format": "ddd, D MMM YY",
                  opts: {
                    disableDays: [ 0, 6 ],
                    minDate: startDate,
                    maxDate: endDate
                  }
               },
    

    Just one last question. I noticed that in order to be able to choose "today" on the datepicker I need to set the "startDate" the day before, or in other words

        startDate.setDate( startDate.getDate()-1 );
    

    There is a specific reason or I am doing something wrong?

    Thanks

  • allanallan Posts: 64,925Questions: 1Answers: 10,753 Site admin
    Answer ✓

    Very nice - thanks for sharing your code with us!

    . I noticed that in order to be able to choose "today" on the datepicker I need to set the "startDate" the day before, or in other words

    That's basically an artefact of the way the dates are being set to exactly midnight for each date in the calendar, and that you are passing in a date / time that is not midnight. If you set the time of your date object to be exactly midnight, it should work as expected.

    Allan

This discussion has been closed.