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

Question

Question

'min' attribute not working on Date Picker

asked on February 28, 2017 Show version history
$(document).ready(function() {
    var todaysDate = new Date();
    var dayofWeek = todaysDate.getDay();
    var year = todaysDate.getFullYear();
    var month = ("0" + (todaysDate.getMonth() + 1)).slice(-2);
    var day = ("0" + todaysDate.getDate()).slice(-2);
    if (dayofWeek == 6) {
        numDaysAdd = 9;
    } else if (dayofWeek == 7) {
        numDaysAdd = 8;
    } else {
        numDaysAdd = 7;
    }
    todaysDate.setDate(todaysDate.getDate() + numDaysAdd);
    var dd = todaysDate.getDate();
    var mm = todaysDate.getMonth() + 1;
    var y = todaysDate.getFullYear();

    var minDate = mm + '/' + dd + '/' + y;

    $('.returnDate input').attr('min', minDate);
});

I am using the above code to set the minimum date in a Return Date date picker field, adding 5 business days. For some reason, starting yesterday as far as i can tell, when i click on the field the date picker disappears and the field becomes a single line text field instead. if i comment out the last line of code, the part that sets the min attribute, everything works fine. This was working without issue up until yesterday.

Any input would be greatly appreciated.

0 0

Answer

SELECTED ANSWER
replied on February 28, 2017

The issue was in the order of the date. See updated code below. I think it may have had something to do with our servers being upgraded/migrated in the last few days and the way dates are handled on that/those machine/s.

$(document).ready(function() {
    var todaysDate = new Date();
    var dayofWeek = todaysDate.getDay();
    if (dayofWeek == 6) {
        numDaysAdd = 9;
    } else if (dayofWeek == 7) {
        numDaysAdd = 8;
    } else {
        numDaysAdd = 7;
    }
    todaysDate.setDate(todaysDate.getDate() + numDaysAdd);
    var dd = todaysDate.getDate();
    var mm = todaysDate.getMonth() + 1; 
  var y = todaysDate.getFullYear();
  var minDate = y + '-' + mm + '-' + dd;
    $('.returnDate input').attr('min', minDate);
});
1 0

Replies

replied on February 27, 2023

I needed to have a maximum date of 15 days after the current date, and a minimum of 15 days before the current date (Current date today:  2/27/2023).

This is what finally worked:

___________________________________________

$(document).ready(function ()
{
// get current date
var d = new Date();
  
// set minimum date
var minDate = moment(d.setDate(d.getDate() - 15)).format('YYYY-MM-DD');
$('.dateField input').attr('min',minDate);
});  

$(document).ready(function ()
{
// get current date
var d = new Date();
  
// set maximum date
var maxDate = moment(d.setDate(d.getDate() + 15)).format('YYYY-MM-DD');
$('.dateField input').attr('max',maxDate);
});

___________________________________________

I am sure there is a better way.  If anyone out there has one, please share.

Thanks!

Christine

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

Sign in to reply to this post.