0

I'm using pickaday.js. I want the user to only be able to select a date between today and the next 15 dates. I have set current date by using javascript new Date() function but I'm unable to figure out how to set max day to next 15 days only. Please check my code below

HTML

<input value="" id="datepicker" type="text" readonly>

SCRIPT

 var currentDate = new Date();
        var day = currentDate.getDate();
        var month = currentDate.getMonth() + 1;
        var year = currentDate.getFullYear();

 var picker = new Pikaday(
            {
              field: document.getElementById('datepicker'),
              firstDay: 1,
              format: "DD/MM/YYYY",
              minDate: new Date(year, month, day),
              maxDate: new Date(2020, 12, 31)
            });
Nautilus
  • 2,236
  • 2
  • 17
  • 33
Kamal
  • 2,140
  • 8
  • 33
  • 61
  • Possible duplicate of [Subtract days from a date in JavaScript](http://stackoverflow.com/questions/1296358/subtract-days-from-a-date-in-javascript) – Pierre Granger Feb 08 '16 at 13:06

3 Answers3

2

just set the maxDate like this

maxDate: new Date(currentDate.getTime() + 15 * 24 * 60 * 60 * 1000)
Sharky
  • 6,154
  • 3
  • 39
  • 72
1

I guess you can do it with setDate :

var minDate = new Date(year, month, day) ;
var maxDate = new Date() ;
maxDate.setDate(minDate.getDate()+15) ;

Regarding Subtract days from a date in JavaScript

Community
  • 1
  • 1
Pierre Granger
  • 1,993
  • 2
  • 15
  • 21
0
var someDate = new Date(); 
someDate.setDate(someDate.getDate() + 15)
var picker = new Pikaday({
  field: document.getElementById('datepicker'),
  firstDay: 1,
  format: "DD/MM/YYYY",
  minDate: new Date(),
  maxDate: someDate 
});

That will add 15 days to your calendar.

Rich
  • 5,603
  • 9
  • 39
  • 61