月の判断には、あまりにも多くの判断条件が含まれます。 Switch Syntaxを使用することをお勧めします
コードは次のとおりです。
コードコピーは次のとおりです。
getdays:function(year、month){
// var aday = [31,28 | 29,31,30,31,30,31,31,31,30,30,31];
// 2月の日データ処理
var reddays = year%4 == 0?29:28、
returndays = '';
var month = month <10?month = '0'+month:month.tostring();
switch(月){
ケース '01':
ケース '03':
ケース '05':
ケース '07':
ケース '08':
ケース '10':
ケース '12':returndays = 31; break;
ケース '04':
ケース '06':
ケース '09':
ケース '11':returndays = 30; break;
ケース '02':returndays = reddays; break;
}
返信日;
}
完全なソースコード:
コードコピーは次のとおりです。
/*著者:laoguoyong
---------------------------------------
日付の3レベルのリンク、範囲選択
---------------------------------------
パラメーター
* [string]ターゲット: '#year、#月、#day';年、月、日のid
* [string]範囲: '2013-02-03,2019-09-21';範囲、正しい形式はxxxx-xx-xxです
---コードを保存するには、正しい日付範囲のパラメーターを渡してください
----エラーデモンストレーション:
(1)範囲: '2013-02-03,2019-9-21'が間違っています、日付形式に注意してください
(2)範囲: '2013-02-03'が間違っています。完全な範囲値を入力してください
(3)範囲: '2013-02-03,2016-02-30'間違っています、2月には30日はありません
(3)範囲: '2013-02-03,2011-02-30'は間違っており、範囲は正しくありません
*
*/
関数gysetdate(opt){
//エレム
var targets = opt.targets.split( '、');
this.eyear = this.getid(ターゲット[0] .slice(1));
this.emonth = this.getid(ターゲット[1] .slice(1));
this.eday = this.getid(ターゲット[2] .slice(1));
if(!this.eyear ||!this.emonth ||!this.eday)return;
//範囲値
var r = opt.range.indexof( '、')、
Astarts = opt.range.slice(0、r).split( ' - ')、// convertに:['2013'、 '05'、 '20']
aends = opt.range.slice(r+1、opt.range.length).split( ' - '); //転換:['2018'、 '08'、 '20']
//番号タイプ
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;
//初期化日
this.seyears({'start':this.startyear、 'end':this.endyear});
this.setmonths({'start':this.startmonth});
this.setdays({'year':this.startyear、 'month':this.startmonth、 'start':this.startday});
//年の選択
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});
};壊す;
case(year == _that.endyear):{
_that.setmonths({'start':1、 'end':_ that.endmonth});
if(_that.endmonth> 1){
_that.setdays({'year':_ that.endyear、 'month':1、 'start':1});
}それ以外{
_that.setdays({'year':_ that.endyear、 'month':1、 'start':1、 'end':_ that.endday});
}
};壊す;
デフォルト:{
_that.setmonths({'start':1});
_that.setdays({'start':1、 'year':year、 'month':1});
}
}
}
//毎月の選択
this.emonth.onchange = function(){
var year = parseint(_that.eyear.options [_that.eyear.selectedindex] .value)、
月= parseint(this.value);
switch(true){
case(year == _ that.endyear && month == _ that.endmonth):{
_that.setdays({'start':1、 'year':year、 'month':month、 'end':_ that.endday});
};壊す;
case(year == _ that.startyear && month == _ that.startmonth):{
_that.setdays({'year':_ that.startyear、 'month':_ that.startmonth、 'start':_ that.startday});
};壊す;
デフォルト:{
_that.setdays({'start':1、 'year':year、 'month':month});
}
}
}
}、
/*年、月、日を設定します
--------------------------------------------
すべてのパラメーター値は数字タイプです
*/
//パラメーター{'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));
}
}、
//パラメーター{'start':xx、 'end':xxx}
//パラメーターの「終了」はオプションで、無視し、12月まで始まります
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));
}
}、
//パラメーター{'start':xx、 'year':xxx、 'month':xx、 'star':xx、 'end':xxx}
//パラメーター「終了」はオプションで、無視してください。今月の終わりまで始まります(月に基づいて判断)
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));
}
}、
/*年と月によると、2016-2、返品は29日(実行年)などの正しい日数を返します
------------------------------------------------------------------------------------------------------------------------------------------------
すべてのパラメーター値は数字タイプです
*/
getdays:function(year、month){
// var aday = [31,28 | 29,31,30,31,30,31,31,31,30,30,31];
// 2月の日データ処理
var reddays = year%4 == 0?29:28、
returndays = '';
var month = month <10?month = '0'+month:month.tostring();
switch(月){
ケース '01':
ケース '03':
ケース '05':
ケース '07':
ケース '08':
ケース '10':
ケース '12':returndays = 31; break;
ケース '04':
ケース '06':
ケース '09':
ケース '11':returndays = 30; break;
ケース '02':returndays = reddays; break;
}
返信日;
}、
/*ツールヘルパー機能
--------------------------------------------
*/
getId:function(id){
document.getElementByID(ID)を返します。
}
}
効果ディスプレイ画像:
効果はかなり良いです。友達、自分で美化し、自分のプロジェクトで使用してください。