El juicio del mes involucra demasiadas condiciones de juicio, si más reducirá en gran medida el rendimiento. Se recomienda usar sintaxis del interruptor
El código es el siguiente:
La copia del código es la siguiente:
GetDays: Función (año, mes) {
// var aday = [31,28 | 29,31,30,31,30,31,31,31,30,30,31];
// Procesamiento de datos del día de febrero
var feddays = año%4 == 0? 29: 28,
returndays = '';
var mes = mes <10? mes = '0'+mes: mes.ToString ();
Switch (mes) {
Caso '01':
Caso '03':
Caso '05':
Caso '07':
Caso '08':
Caso '10':
caso '12': returndays = 31; ruptura;
Caso '04':
Caso '06':
Caso '09':
caso '11': returndays = 30; ruptura;
caso '02': returndays = Feddays; break;
}
Return Returndays;
}
Código fuente completo:
La copia del código es la siguiente:
/* Autor: Lauguoyong
------------------------------
Vínculo de tres niveles de fechas, selección de rango
------------------------------
parámetro
* [String] Objetivos: '#año,#mes,#day'; identificación del año, mes, día
* [String] Rango: '2013-02-03,2019-09-21'; Rango, el formato correcto es xxxx-xx-xx
--- Para guardar código, pase el parámetro de rango de fecha correcto
---- demostración de errores:
(1) Rango: '2013-02-03,2019-9-21' es incorrecto, preste atención al formato de fecha
(2) Rango: '2013-02-03' es incorrecto, ingrese el valor de rango completo
(3) Rango: '2013-02-03,2016-02-30' Está mal, no hay 30 días en febrero
(3) Rango: '2013-02-03,2011-02-30' es incorrecto, el rango es incorrecto
*
*/
función gysetdate (opt) {
// Elem
Var Targets = opt.targets.split (',');
this.eyear = this.getId (objetivos [0] .slice (1));
this.emonth = this.getId (objetivos [1] .slice (1));
this.eday = this.getId (objetivos [2] .slice (1));
if (! this.eyear ||! this.emonth ||! this.eday) return;
// valor de rango
var r = opt.range.indexof (','),
astarts = opt.range.slice (0, r) .split ('-'), // Convertir a: ['2013', '05', '20']]
aends = opt.range.slice (r+1, opt.range.length) .split ('-'); // Convertir a: ['2018', '08', '20']
// Tipo de número
this.startyear = parseInt (astarts [0], 10);
this.startMonth = parseInt (astarts [1], 10);
this.startday = parseint (astarts [2], 10);
this.endyear = parseInt (AIENS [0], 10);
this.endmonth = parseInt (atiende [1], 10);
this.endday = parseInt (atiende [2], 10);
this.init ();
}
Gysetdate.prototype = {
init: function () {
var _that = this;
// Fecha de inicialización
this.setyears ({'inicio': this.Startyear, 'end': this.endyear});
this.setMonths ({'inicio': this.StartMonth});
this.setdays ({'año': this.Startyear, 'mes': this.StartMonth, 'inicio': this.StartDay});
// Elección del año
this.eyear.onchange = function () {
var año = parseint (this.value);
Switch (True) {
case (año == _that.Startyear): {
_THAT.SetMonths ({'inicio': _ that.StartMonth});
_that.setdays ({'año': _ that.Startyear, 'mes': _ that.StartMonth, 'inicio': _ que.Startday});
};romper;
case (año == _that.endyear): {
_that.setmonths ({'inicio': 1, 'end': _ that.endmonth});
if (_that.endmonth> 1) {
_that.setdays ({'año': _ that.endyear, 'mes': 1, 'inicio': 1});
}demás{
_that.setdays ({'año': _ that.endyear, 'mes': 1, 'inicio': 1, 'final': _ that.endday});
}
};romper;
por defecto:{
_that.setMonths ({'inicio': 1});
_that.setdays ({'inicio': 1, 'año': año, 'mes': 1});
}
}
}
// Selección mensual
this.emonth.onchange = function () {
var año = parseInt (_that.eyear.options [_that.eyear.selectedIndex] .Value),
mes = parseint (this.value);
Switch (True) {
case (año == _ that.endyear && mes == _ that.endmonth): {
_that.setdays ({'inicio': 1, 'año': año, 'mes': mes, 'final': _ que.endday});
};romper;
case (año == _ that.startyear && mes == _ that.StartMonth): {
_that.setdays ({'año': _ that.Startyear, 'mes': _ that.StartMonth, 'inicio': _ que.Startday});
};romper;
por defecto:{
_that.setdays ({'inicio': 1, 'año': año, 'mes': mes});
}
}
}
},
/*Establecer año, mes, día
----------------------------------
Todos los valores de parámetros son de tipo de número
*/
// Parámetros {'inicio': xx, 'end': xxx}
setyears: function (opt) {
this.eyear.innerhtml = '';
para (var n = opt.start; n <= opt.end; n ++) {
this.eyear.add (nueva opción (n, n));
}
},
// Parámetros {'inicio': xx, 'end': xxx}
// El parámetro 'End' es opcional, lo ignora y comienza hasta diciembre
setmonths: function (opt) {
this.emonth.innerhtml = '';
VAR Meses = Opt.end || 12;
para (var n = opt.start; n <= meses; n ++) {
if (n <10) n = '0'+n;
this.emonth.add (nueva opción (n, n));
}
},
// Parámetros {'inicio': xx, 'año': xxx, 'mes': xx, 'star': xx, 'end': xxx}
// El parámetro 'End' es opcional, lo ignora y comienza hasta finales de este mes (juzgado en función del mes)
setdays: function (opt) {
this.eday.innerhtml = '';
var days = Opt.end || this.getdays (Opt.Year, Opt.Month);
para (var n = opt.start; n <= días; n ++) {
if (n <10) n = '0'+n;
this.eday.add (nueva opción (n, n));
}
},
/* Según año y mes, devuelva el número correcto de días, como 2016-2, el retorno es de 29 días (año de ejecución)
--------------------------------------------------------------
Todos los valores de parámetros son de tipo de número
*/
GetDays: Función (año, mes) {
// var aday = [31,28 | 29,31,30,31,30,31,31,31,30,30,31];
// Procesamiento de datos del día de febrero
var feddays = año%4 == 0? 29: 28,
returndays = '';
var mes = mes <10? mes = '0'+mes: mes.ToString ();
Switch (mes) {
Caso '01':
Caso '03':
Caso '05':
Caso '07':
Caso '08':
Caso '10':
caso '12': returndays = 31; ruptura;
Caso '04':
Caso '06':
Caso '09':
caso '11': returndays = 30; ruptura;
caso '02': returndays = Feddays; break;
}
Return Returndays;
},
/* Función de ayuda para la herramienta
----------------------------------
*/
getId: function (id) {
return document.getElementById (id);
}
}
Imagen de visualización de efectos:
El efecto es bastante bueno. Amigos, por favor, embílelo usted mismo y úselo en su propio proyecto.