The month judgment involves too many judgment conditions, if else will greatly reduce performance. It is recommended to use switch syntax
The code is as follows:
The code copy is as follows:
getDays:function(year,month){
// var aDay = [31,28|29,31,30,31,30,31,31,31,30,30,31];
// February’s day data processing
var FedDays = year%4==0?29:28,
returnDays = '';
var month = month<10?month = '0'+month:month.toString();
switch(month){
case '01':
case '03':
case '05':
case '07':
case '08':
case '10':
case '12': returnDays = 31;break;
case '04':
case '06':
case '09':
case '11': returnDays = 30;break;
case '02': returnDays = FedDays;break;
}
return returnDays;
}
Complete source code:
The code copy is as follows:
/* author:laoguoyong
------------------------------
Three-level linkage of dates, range selection
------------------------------
parameter
* [String] targets:'#year,#month,#day' ; id of year, month, day
* [String] range:'2013-02-03,2019-09-21'; range, correct format is xxxx-xx-xx
---To save code, please pass in the correct date range parameter
----Error demonstration:
(1) range:'2013-02-03,2019-9-21' is wrong, pay attention to the date format
(2) range:'2013-02-03' is wrong, please enter the complete range value
(3) range:'2013-02-03,2016-02-30' It's wrong, there are no 30 days in February
(3) range:'2013-02-03,2011-02-30' is wrong, the range is incorrect
*
*/
function GySetDate(opt){
//elem
var targets = opt.targets.split(',');
this.eYear = this.getId(targets[0].slice(1));
this.eMonth = this.getId(targets[1].slice(1));
this.eDay = this.getId(targets[2].slice(1));
if(!this.eYear||!this.eMonth||!this.eDay) return;
//Range value
var r = opt.range.indexOf(','),
aStarts = opt.range.slice(0,r).split('-'), // Convert to: ['2013','05','20']
aEnds = opt.range.slice(r+1,opt.range.length).split('-'); // Convert to: ['2018','08','20']
//Number type
this.startYear = parseInt(aStarts[0],10);
this.startMonth = parseInt(aStarts[1],10);
this.startDay = parseInt(aStarts[2],10);
this.endYear = parseInt(aEnds[0],10);
this.endMonth = parseInt(aEnds[1],10);
this.endDay = parseInt(aEnds[2],10);
this.init();
}
GySetDate.prototype = {
init:function(){
var _that = this;
// Initialization date
this.setYears({'start':this.startYear,'end':this.endYear});
this.setMonths({'start':this.startMonth});
this.setDays({'year':this.startYear,'month':this.startMonth,'start':this.startDay});
// Year's choice
this.eYear.onchange = function(){
var year = parseInt(this.value);
switch(true){
case (year == _that.startYear):{
_that.setMonths({'start':_that.startMonth});
_that.setDays({'year':_that.startYear,'month':_that.startMonth,'start':_that.startDay});
};break;
case (year == _that.endYear):{
_that.setMonths({'start':1,'end':_that.endMonth});
if(_that.endMonth>1){
_that.setDays({'year':_that.endYear,'month':1,'start':1});
}else{
_that.setDays({'year':_that.endYear,'month':1,'start':1,'end':_that.endDay});
}
};break;
default:{
_that.setMonths({'start':1});
_that.setDays({'start':1,'year':year,'month':1});
}
}
}
// Monthly selection
this.eMonth.onchange = function(){
var year = parseInt(_that.eYear.options[_that.eYear.selectedIndex].value),
month = parseInt(this.value);
switch(true){
case (year==_that.endYear&&month==_that.endMonth):{
_that.setDays({'start':1,'year':year,'month':month,'end':_that.endDay});
};break;
case (year==_that.startYear&&month==_that.startMonth):{
_that.setDays({'year':_that.startYear,'month':_that.startMonth,'start':_that.startDay});
};break;
default:{
_that.setDays({'start':1,'year':year,'month':month});
}
}
}
},
/*Set year, month, day
----------------------------------
All parameter values are of Number type
*/
// Parameters {'start':xx,'end':xxx}
setYears:function(opt){
this.eYear.innerHTML = '';
for(var n=opt.start;n<=opt.end;n++){
this.eYear.add(new Option(n,n));
}
},
// Parameters {'start':xx,'end':xxx}
// The parameter 'end' is optional, ignore it, and it starts until December
setMonths:function(opt){
this.eMonth.innerHTML = '';
var months = opt.end || 12;
for(var n=opt.start;n<=months;n++){
if(n<10) n = '0'+n;
this.eMonth.add(new Option(n,n));
}
},
// Parameters {'start':xx,'year':xxx,'month':xx,'star':xx,'end':xxx}
// The parameter 'end' is optional, ignore it, and it starts until the end of this month (judged based on the month)
setDays:function(opt){
this.eDay.innerHTML = '';
var days = opt.end || this.getDays(opt.year,opt.month);
for(var n=opt.start;n<=days;n++){
if(n<10) n = '0'+n;
this.eDay.add(new Option(n,n));
}
},
/* According to year and month, return the correct number of days, such as 2016-2, return is 29 days (run year)
--------------------------------------------------------------
All parameter values are of Number type
*/
getDays:function(year,month){
// var aDay = [31,28|29,31,30,31,30,31,31,31,30,30,31];
// February’s day data processing
var FedDays = year%4==0?29:28,
returnDays = '';
var month = month<10?month = '0'+month:month.toString();
switch(month){
case '01':
case '03':
case '05':
case '07':
case '08':
case '10':
case '12': returnDays = 31;break;
case '04':
case '06':
case '09':
case '11': returnDays = 30;break;
case '02': returnDays = FedDays;break;
}
return returnDays;
},
/* Tool helper function
----------------------------------
*/
getId:function(id){
return document.getElementById(id);
}
}
Effect display picture:
The effect is pretty good. Friends, please beautify it yourself and use it in your own project.