Home > Software design >  How to set two input month date type in javascript and make a limitation in the second input month t
How to set two input month date type in javascript and make a limitation in the second input month t

Time:12-03

I have two problems with input date I have a two input type months. one of them is start date another is the end date. First I want to choose start date then automatically the end date change to start date time. because it makes easy for our client to choose from the start point.

Let me say an example. In start date, after for example I choose February 2020, in the second input date change to February 2020. It doesn't mean the second input date definitely will be February 2020! Maybe our client wants to choose December 2020 But it really makes easy for them to choose it.

The second thing is that the second input should just at least 12 month enable after February 2020. Before February 2020, and after 12 month in this example January 2021 should be disabled. I really don't know how to write it :(

<input type="month" id="start1" name="start1" min="2010-00" max="2040-00" value="2022-09" onclick="myFunction()" >

 <input type="month" id="end1" name="end1"    min="2010-00" max="2040-00" value="2022-09" >

CodePudding user response:

I think the answer is:

    <td>
<input type="month" id="start1" name="start1"
       min="2010-00" max="2040-00" value="2022-09" onchange="startFunction('start1','end1');" disabled>
</td>
//-----------------------------------------------------------------------------
function startFunction(startn,endn){
document.getElementById(endn).value=document.getElementById(startn).value;
}

function endFunction(startn,endn){
let x=document.getElementById(startn).value;
let y=document.getElementById(endn).value;

const StartArray1 = x.split("-");
const EndArray2 = y.split("-");

let startintyear = parseInt(StartArray1[0]);
let startintmonth = parseInt(StartArray1[1]);
let endintyear = parseInt(EndArray2[0]);
let endintmonth = parseInt(EndArray2[1]);


if(startintyear>endintyear){
alert("Wrong! Your Start Year is Bigger than end Year.");

}else if((startintyear==endintyear) && (startintmonth>endintmonth)){
alert("Wrong! End Month is Smaller than Start Month. ");
}

if(endintyear>startintyear){
let  diffyear =  endintyear- startintyear;
 if((diffyear*12) ((endintmonth-startintmonth) 1)>12){
     alert("Wrong! The duration is bigger than 12 month!");
 }
}

}
  • Related