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

Question

Question

date restriction based on status variable.

asked on July 30, 2024 Show version history

I have the following code to sets the minimum date for a date field (Class is PropDate).

$(function() {
  $(document).ready(function () {
    
   var todaysDate = new Date(); // Gets today's date
    
    // Min 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 min date value for the calendar to be today's date
    $('.PropDate input').attr('min',minDate);
 
  });
});

I ended up having to add a status field (Task Scheduling) for items that are to be delayed more than 30 days. If "Delay" is picked from the status field, the date minimum should be 30 days from the current date, but if it is not chosen, the minimum date from the code is maintained. Can anyone help me adjust this code to function in that manner? Sorry, not great with coding.

0 0

Answer

APPROVED ANSWER SELECTED ANSWER
replied on July 30, 2024 Show version history

Something like this should work.  This is assuming that your radio button has a class name of "myRadioButton" (lines 5 and 12) and the value for the delay 30 days option on the radio button is "Delay" (line 12).

$(document).ready(function () {
    
  //Run the function when the form is loaded and when the radio button changes.
  SetDateMinimum();
  $('.myRadioButton').change(SetDateMinimum);
  
  //Function that updates the minimum date on the date field.
  function SetDateMinimum() {
    var todaysDate = new Date(); // Gets today's date
    
    //If the radio button is set to a value of Delay, add 30 days to the date.
    if ($('.myRadioButton input:checked').val() == 'Delay') {
      todaysDate.setDate(todaysDate.getDate() + 30);
    }
    
    // Min 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 min date value for the calendar to be today's date
    $('.PropDate input').attr('min',minDate);
  }
 
});

 

1 0
replied on July 31, 2024

That worked! 

Thank you. I always appreciate your help, which you have provided many times. smiley

1 0
replied on July 31, 2024

Happy to be of assistance.  Have a great day!

0 0

Replies

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

Sign in to reply to this post.