Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

147
Views
Disabling the dates according to previous date selection

I'm new to JavaScript. So please don't go harsh on me.

After the StartDate gets selected, the ExpectedEndDate and the EndDate must start with StartDate + 1 day and end in 6 months. Other dates must be disabled in the ExpectedEndDate and EndDate.

Here the futureInput.Max works fine. But the futureInput.Min doesn't work. I couldn't find where it went wrong.

$(document).ready(()=>{

                var startInput = $('#StartDate')[0];
                var expectedEndInput = $('#ExpectedEndDate')[0];
                var endInput = $('#EndDate')[0];

                startInput.addEventListener('change', (evt)=>{
                    
                    var EndTime = new Date($("#StartDate").val());
                    var dd = String(EndTime.getDate() + 1);
                    var mm = String(EndTime.getMonth() + 6);
                    var yyyy = EndTime.getFullYear();

                    var MaxTime = yyyy + '-' + mm + '-' + dd;

                    var StartTime = new Date($("#StartDate").val());
                    var dd = String(StartTime.getDate() + 1);
                    var mm = String(StartTime.getMonth() + 1);
                    var yyyy = StartTime.getFullYear();

                    var MinTime = yyyy + '-' + mm + '-' + dd;

                    debugger
                    [expectedEndInput, endInput].forEach((futureInput)=> {
                    futureInput.min = MinTime;
                    futureInput.max = MaxTime;
                    debugger
                    })
                    
                })
            })
<link href="https://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet">
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script src="https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>



<input type="date" id="StartDate" />
<input type="date" id="ExpectedEndDate" />
<input type="date" id="EndDate" />
about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

In order to react to every change of the <input id="StartDate">, we need to set a change event listener for it.

function daysToMilliseconds(days) {
    return days * 24 * 60 * 60 * 1000;
}

function millisecondsToDateString(milliseconds) {
    // to convert Date to yyyy-mm-dd use Swedish locale
    return new Date(milliseconds).toLocaleDateString('sv-SE');
}

$(document).ready(()=>{

  var startInput = $('#StartDate')[0];
  var expectedEndInput = $('#ExpectedEndDate')[0];
  var endInput = $('#EndDate')[0];
  
  startInput.addEventListener('change', (evt)=>{
    // called every time the startInput's date value changes
  
    // get start date as milliseconds since 1970 New Year
    var startTime = startInput.valueAsNumber;
    
    // startDate + 1 day
    var minTime = startTime + daysToMilliseconds(1);
    // startDate + 180 days (6 months)
    var maxTime = startTime + daysToMilliseconds(30 * 6);
    
    [expectedEndInput,endInput].forEach((futureInput)=> {
      futureInput.min = millisecondsToDateString(minTime);
      futureInput.max = millisecondsToDateString(maxTime);
    });
  })
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="date" id="StartDate" />
<input type="date" id="ExpectedEndDate" />
<input type="date" id="EndDate" />

Refer to this SO answer for conversion of Date objects to yyyy-mm-dd. Only values of yyyy-mm-dd are accepted for input date min & max.

about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!