You are viewing limited content. For full access, please sign in.

Question

Question

Display date in the future

asked on June 19, 2019

Is there a way to have the calendar display 2 weeks from today's date? 

I can get it so the date picker does not allow you to select anything less than today's date, however I would like it to limit the user to not be able to pick anything from 2 weeks from today's date.

 

$(function() {
  $(document).ready(function () {
   var todaysDate = new Date(); // Gets today's date
    // Max date attribute is in "YYYY-MM-DD".  Need to format today's date accordingly
    var year = todaysDate.getFullYear();                        // YYYY
    var month = ("0" + (todaysDate.getMonth() + 1)).slice(-2);  // MM
    var day = ("0" + todaysDate.getDate()).slice(-2);           // DD
    var minDate = (year +"-"+ month +"-"+ day); // Results in "YYYY-MM-DD" for today's date 
    // Now to set the max date value for the calendar to be today's date
    $('.calendarDate input').attr('min',minDate);  
  });
});
 

 

Any help is greatly appreciated.  

0 0

Replies

replied on June 19, 2019

You would set the max attribute. To do that, you would add 2 weeks to a date object and use that similar to how you set the "min" date.

Something like,

var futureDate = new Date();
futureDate.setDate(now.getDate()+14);

 

0 0
replied on June 20, 2019 Show version history

Thank you Jason.  I am sorry for the questions, but I am not a programmer by any means.  :) 

Am I replacing these 2 lines with the following?

 var todaysDate = new Date();

$('.calendarDate input').attr('min',minDate);

 

0 0
replied on June 20, 2019

This should do the trick,

$(document).ready(function(){
  // get date object
  var d = new Date();
  var minDate = formatDate(d);
  
  // get future date
  d.setDate(d.getDate()+14);
  var maxDate = formatDate(d);
  
  // set min/max dates  
  $('.calendarDate input').attr({'min':minDate,'max':maxDate});
});

function formatDate(date){
  var ymd = [,,];
  
  // get date values
  ymd[0] = date.getFullYear();
  ymd[1] = ('0' + (date.getMonth() + 1)).slice(-2);
  ymd[2] = ('0' + date.getDate()).slice(-2);
  
  // return formatted date
  return ymd.join('-');
}

This handles both the min and max dates.

2 0
replied on June 20, 2019

This is awesome!  Thank you.  

0 0
replied on June 20, 2019

Thank you!!

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.