The determination of the month, because it involves too much judgment conditions, if you use if else will greatly reduce performance, the proposed switch syntax
/* Author:laoguoyong
------------------------------
Date three level linkage, range selection
------------------------------
Parameters
* [String] Targets: ' #year, #month, #day '; ID of year, month, day
* [String] Range: ' 2013-02-03,2019-09-21 '; Range, correctly formatted as XXXX-XX-XX
----to save code, pass in the correct date range parameter
----Error Demonstration:
(1) Range: ' 2013-02-03,2019-9-21 ' is wrong, note the date format
(2) Range: ' 2013-02-03 ' is wrong, please enter the full range of values
(3) Range: ' 2013-02-03,2016-02-30 ' is wrong, February is not 30 days
(3) Range: ' 2013-02-03,2011-02-30 ' is wrong, the range is wrong
*
*/
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 ('-'); 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 selection
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});
}
}
}
Month 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
----------------------------------
Parameter values are of type number
*/
parameter {' 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));
}
},
parameter {' Start ': xx, ' End ': xxx}
Parameter ' end ' is optional, ignored, then begins 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));
}
},
parameter {' Start ': xx, ' year ': xxx, ' month ': xx, ' star ': xx, ' End ': xxx}
The parameter ' end ' is optional, ignored, and begins at the beginning of the month (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, month, return the correct number of days, such as 2016-2, return is 29 days (run year)
--------------------------------------------------------------
Parameter values are of type number
*/
Getdays:function (year,month) {
var aday = [31,28|29,31,30,31,30,31,31,30,31,30,31];
Number of days data processing in February
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 ' a ': Returndays = 31;break;
Case ' 04 ':
Case ' 06 ':
Case ' 09 ':
Case ' one ': Returndays = 30;break;
Case ' a ': Returndays = feddays;break;
}
return returndays;
},
/* Tool Auxiliary function
----------------------------------
*/
Getid:function (ID) {
return document.getElementById (ID);
}
}
The effect is not bad, small partners to beautify themselves, to use their own projects to go.