<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>日期联动选择器</title>
<script type="text/javascript">
var $ = function (id) {
return "string" == typeof id ? document.getElementById(id) : id;
};
function addEventHandler(oTarget, sEventType, fnHandler) {
if (oTarget.addEventListener) {
oTarget.addEventListener(sEventType, fnHandler, false);
} else if (oTarget.attachEvent) {
oTarget.attachEvent("on" + sEventType, fnHandler);
} else {
oTarget["on" + sEventType] = fnHandler;
}
};
var Class = {
create: function() {
return function() {
this.initialize.apply(this, arguments);
}
}
}
var Extend = function(destination, source) {
for (var property in source) {
destination[property] = source[property];
}
return destination;
}
var DateSelector = Class.create();
DateSelector.prototype = {
initialize: function(oYear, oMonth, oDay,oHour,oMinute,oSecond, options) {
this.SelYear = $(oYear);//年选择对象
this.SelMonth = $(oMonth);//月选择对象
this.SelDay = $(oDay);//日选择对象
this.SelHour = $(oHour);
this.SelMinute = $(oMinute);
this.SelSecond = $(oSecond);
this.SetOptions(options);
var dt = new Date(), iMonth = parseInt(this.options.Month), iDay = parseInt(this.options.Day),iMinYear = parseInt(this.options.MinYear), iMaxYear = parseInt(this.options.MaxYear);
this.Year = parseInt(this.options.Year) || dt.getFullYear();
this.Month = 1 <= iMonth && iMonth <= 12 ? iMonth : dt.getMonth() + 1;
this.Day = iDay > 0 ? iDay : dt.getDate();
this.Hour = dt.getHours();
this.Minute = dt.getMinutes();
this.Second = dt.getSeconds();
this.MinYear = iMinYear && iMinYear < this.Year ? iMinYear : this.Year;
this.MaxYear = iMaxYear && iMaxYear > this.Year ? iMaxYear : this.Year;
this.onChange = this.options.onChange;
//年设置
this.SetSelect(this.SelYear, this.MinYear, this.MaxYear - this.MinYear + 1, this.Year - this.MinYear);
//月设置
this.SetSelect(this.SelMonth, 1, 12, this.Month - 1);
//日设置
this.SetDay();
this.SetSelect(this.SelHour, 1, 24, this.Hour - 1);
this.SetSelect(this.SelMinute, 1, 60, this.Minute - 1);
this.SetSelect(this.SelSecond, 0, 60, this.Second - 1);
var oThis = this;
//日期改变事件
addEventHandler(this.SelYear, "change", function(){
oThis.Year = oThis.SelYear.value; oThis.SetDay(); oThis.onChange();
});
addEventHandler(this.SelMonth, "change", function(){
oThis.Month = oThis.SelMonth.value; oThis.SetDay(); oThis.onChange();
});
addEventHandler(this.SelDay, "change", function(){ oThis.Day = oThis.SelDay.value; oThis.onChange(); });
},
//设置默认属性
SetOptions: function(options) {
this.options = {//默认值
Year: 0,//年
Month: 0,//月
Day: 0,//日
MinYear: 0,//最小年份
MaxYear: 0,//最大年份
onChange: function(){}//日期改变时执行
};
Extend(this.options, options || {});
},
//日设置
SetDay: function() {
//取得月份天数
var daysInMonth = new Date(this.Year, this.Month, 0).getDate();
if (this.Day > daysInMonth) { this.Day = daysInMonth; };
this.SetSelect(this.SelDay, 1, daysInMonth, this.Day - 1);
},
//select设置
SetSelect: function(oSelect, iStart, iLength, iIndex) {
//添加option
oSelect.options.length = iLength;
for (var i = 0; i < iLength; i++) {
//oSelect.options[i].text = oSelect.options[i].value = iStart + i;
var tmp = iStart + i
oSelect.options[i].value = tmp ;
if(tmp < 10){oSelect.options[i].text = '0'+ tmp; }
else{oSelect.options[i].text = tmp; }
}
//设置选中项
oSelect.selectedIndex = iIndex;
},
GetDateN:function(){
var y = parseInt(this.SelYear.options[this.SelYear.selectedIndex].text);
var m = parseInt(this.SelMonth.options[this.SelMonth.selectedIndex].text);
var d = parseInt(this.SelDay.options[this.SelDay.selectedIndex].text);
var h = parseInt(this.SelHour.options[this.SelHour.selectedIndex].text);
var mm = parseInt(this.SelMinute.options[this.SelMinute.selectedIndex].text);
var ss = parseInt(this.SelSecond.options[this.SelSecond.selectedIndex].text);
alert(m);
var tempdate = new Date(y,m,d,h,mm,ss);
return tempdate;
}
};
</script>
</head>
<body>
From:<select id="f_idMonth"></select>
<select id="f_idDay"></select>
<select id="f_idYear"></select>
<select id="f_idHour"></select>
<select id="f_idMinute"></select>
<select id="f_idSecond"></select>
<br>
To:<select id="t_idMonth"></select>
<select id="t_idDay"></select>
<select id="t_idYear"></select>
<select id="t_idHour"></select>
<select id="t_idMinute"></select>
<select id="t_idSecond"></select>
<br />
你选择的日期:<span id="idShow"></span>
<script>
var ds = new DateSelector("f_idMonth", "f_idDay", "f_idYear","f_idHour", "f_idMinute", "f_idSecond", {
MaxYear: new Date().getFullYear(),
MinYear: new Date().getFullYear()-10,
onChange: function(){ $("idShow").innerHTML = this.Year + "/" + this.Month + "/" + this.Day; }
});
var da = new DateSelector("t_idMonth", "t_idDay", "t_idYear","t_idHour", "t_idMinute", "t_idSecond", {
MaxYear: new Date().getFullYear(),
MinYear: new Date().getFullYear()-10,
onChange: function(){ $("idShow").innerHTML = this.Year + "/" + this.Month + "/" + this.Day; }
});
ds.onChange();
function tst(){
alert('from : ' + ds.GetDateN() + 'to :' + da.GetDateN() );
}
</script>
<button onclick='tst()'>Tst</button>
</body>
</html>
分享到:
相关推荐
JavaScript 日期联动选择器
js关于日期联动的代码 使用js动态添加。
支持日期联动,可以自己修改
支持开始时间和结束时间联动,从VUE和element UI框架中拆分出来的。可以作为一个插件,单独引入到你需要的单个页面中。。UI界面很美观。功能强大。
基于javascript bootstrap实现生日日期联动选择_.docx
日期联动,利用jquery实现的日期联动,实用又简单
NULL 博文链接:https://onestopweb.iteye.com/blog/2365816
ajax的使用,通过这个例子,可以了解ajax的流程以及基本的使用方法
主要介绍了jQuery实现日期联动效果的方法,结合实例形式分析了jQuery针对日期及页面元素动态操作的相关技巧,需要的朋友可以参考下
年月日 日期联动 简单容易理解 判断年和月 决定 日 取得的天书
javascript 日期联动选择器,简单的看了下代码非常不错,都考虑到了标准,兼容性也不错。学习js的朋友有福了。
在excel电子表格内,通过微调控件,调整年月后,考勤表内日期自动变动,利用条件格式,自动标注周六日,以示区别。表可以根据自己情况加以调整,
严格的三级联动日期下拉菜单,自动判断闰年,通过年月自动显示合法的 日期
虽然两个组件库给出的相关组件也很棒,但是有时候确实不是太好用,不太明白为什么很多组件库都抛弃了日期联动选择。因此考虑自己动手做一个。 将时间戳转换成日期格式 // timestamp 为时间戳 new Date(timestamp) /...
JS年月日三级联动下拉框日期选择代码,默认当前日期,代码附带详细中文注释,供大家参考。
本文实例为大家分享了js实现日期联动选择的相关代码,适用于生日的选择,供大家参考,具体内容如下 实现目标:年月日三个select 输入框,以及一个hidden的input,通过js获取input的值,如果有值切是日期格式,年月日...
月份的判定,由于涉及到过多了判定条件,如果用if else会大大降低性能,建议用switch 语法 代码如下: 代码如下: getDays:function(year,month){ // var aDay = [31,28|29,31,30,31,30,31,31,30,31,30,31];...