Just made a date addition and subtraction to prevent later use. Make a small record first
I met two small problems during the whole process (1. I confused the Date of Day acquisition with the Day of week acquisition, 2. I met NaN in judgment)
1.false, null, 0, "", undefined are all easy to judge. Use = = = directly
But NaN can't use = = =
> NaN === NaN; false //Always return false
2. There is a global function isNaN() in JS, but there is a problem with this function. It always implicitly converts the value in the parameter into a number and then makes a judgment. In this way, it returns true when judging many obviously non NaN values:
> isNaN("foo") true
resolvent:
function myIsNaN(value) { return value !== value; } //Before using isNaN(), check whether the value is a numeric type, so as to avoid the problem of implicit conversion: function myIsNaN2(value) { return typeof value === 'number' && isNaN(value); }
-------------------------------First, let's see the calculation interface. Let's use language to describe it: deposit period + value date () = due date
The designed (date control, loss of focus event) doesn't say much, just plug in the code
//Value date focus trigger function startonfours(){ var endDate=$dp.$('endDate'); console.log(123) //1. Get the value of the storage period text box, and perform string segmentation to determine the storage period var bankSaveYear= $("#bankSaveYear").find("option:selected").text(); var bankSaveYearval= $("#bankSaveYear").find("option:selected").val(); console.log(bankSaveYearval) if(bankSaveYearval !=""){ //Time to obtain value date var qixiri =$("#startDate").val(); //Judge whether there is value if(bankSaveYear!= null && qixiri !=null){ //Now we have two values--------------------------- var saveYearnum = bankSaveYear.replace(/[^0-9]/ig,""); var saveYearchar = bankSaveYear.match(/[\u4e00-\u9fa5]/g).join(""); //Obtain year, month and day respectively var curtDate = new Date(qixiri); var curtYear = parseInt(curtDate.getFullYear()); var curtMonth = parseInt(curtDate.getMonth()+1); var curtTian = parseInt(curtDate.getDate()); //Latest date var now=""; console.log(curtYear) console.log(curtMonth) var numTian = (curtTian) -1; //Year and month-1 of interest date if(curtMonth ===curtMonth && !isNaN(curtMonth)){ if(saveYearchar =='Months' && saveYearchar!=null){ //Month: 1. Current month + to year after December + 1 //Current month + term (month) var numyue = (curtMonth)+parseInt(saveYearnum); console.log(numyue +"--------"+curtMonth+"---------"+saveYearnum) while(numyue>12){ curtYear++; numyue -=12; } //Accurately calculate the date now = curtYear+"-"+numyue+"-"+numTian; console.log(now); $("#endDate").val(now); } //Judgement year if(saveYearchar =='year' && saveYearchar!=null){ //Current year + term (year) var numYear = (curtYear)+parseInt(saveYearnum); console.log(numyue +"--------"+curtMonth+"---------"+saveYearnum) //Accurately calculate the date now = numYear+"-"+curtMonth+"-"+numTian; console.log(now); $("#endDate").val(now); } //Judgement day if(saveYearchar =='day' && saveYearchar!=null){ //Days: 1. Current days + to 1,3,5,7,8,10,12: 31 days per month, 4,6,9,11: 30 days, February: 29 days, //At the end of the month +, then month + 1, December 29 + 1, then year + 1 //Current days + term (days) - month + 1 - year + 1 var numDate = (curtTian)+parseInt(saveYearnum); console.log(numDate+"=====================================") if(curtMonth ==1 || curtMonth ==3 || curtMonth ==5 || curtMonth ==7 || curtMonth ==8 || curtMonth ==10 || curtMonth ==12){ while(numDate>31){ curtMonth++; numDate-=31 while(curtMonth>12){ curtYear++; curtMonth-=12; } } } //Accurately calculate the date numDate-1: arrival date-1 day var daoxiri =(numDate -1); now = curtYear+"-"+curtMonth+"-"+daoxiri; console.log(now); $("#endDate").val(now); } } } }else{ showAlert("Please select the deposit period!","Reminder") }
Done.