当前位置: 移动技术网 > IT编程>开发语言>JavaScript > 转一个日期输入控件,支持FF

转一个日期输入控件,支持FF

2019年08月01日  | 移动技术网IT编程  | 我要评论
<html>
<head>
<title>日期选择器</title>
<script type="text/javascript">
/**
* 返回日期
* @param d the delimiter
* @param p the pattern of your date
* @author  xinge(修改)
*/
string.prototype.todate = function(x, p) {
 if(x == null) x = "-";
 if(p == null) p = "ymd";
 var a = this.split(x);
 var y = parseint(a[p.indexof("y")]);
 //remember to change this next century ;)
 if(y.tostring().length <= 2) y += 2000;
 if(isnan(y)) y = new date().getfullyear();
 var m = parseint(a[p.indexof("m")]) - 1;
 var d = parseint(a[p.indexof("d")]);
 if(isnan(d)) d = 1;
 return new date(y, m, d);
}

/**
* 格式化日期
* @param   d the delimiter
* @param   p the pattern of your date
* @author  xinge(修改)
*/
date.prototype.format = function(style) {
 var o = {
   "m+" : this.getmonth() + 1, //month
   "d+" : this.getdate(),      //day
   "h+" : this.gethours(),     //hour
   "m+" : this.getminutes(),   //minute
   "s+" : this.getseconds(),   //second
   "w+" : "天一二三四五六".charat(this.getday()),   //week
   "q+" : math.floor((this.getmonth() + 3) / 3),  //quarter
   "s"  : this.getmilliseconds() //millisecond
 }
 if(/(y+)/.test(style)) {
   style = style.replace(regexp.$1,
   (this.getfullyear() + "").substr(4 - regexp.$1.length));
 }
 for(var k in o){
   if(new regexp("("+ k +")").test(style)){
     style = style.replace(regexp.$1,
       regexp.$1.length == 1 ? o[k] :
       ("00" + o[k]).substr(("" + o[k]).length));
   }
 }
 return style;
};

/**
* 日历类
* @param   beginyear 1990
* @param   endyear  񎧚
* @param   lang     ŀ(中文)|1(英语) 可自由扩充
* @param   dateformatstyle  "yyyy-mm-dd";
* @version 2007-03-16
* @author  xinge(修改)
* @update
*/
function calendar(lang,beginyear,endyear,dateformatstyle) {
 this.beginyear = 1990;
 this.endyear = 2010;
 this.lang = 0;  //0(中文) | 1(英文)
 this.dateformatstyle = "yyyy-mm-dd";

 if (beginyear != null && endyear != null){
   this.beginyear = beginyear;
   this.endyear = endyear;
 }
 if (lang != null){
   this.lang = lang
 }

 if (dateformatstyle != null){
   this.dateformatstyle = dateformatstyle
 }

 this.datecontrol = null;
 this.panel = this.getelementbyid("calendarpanel");
 this.form  = null;

 this.date = new date();
 this.year = this.date.getfullyear();
 this.month = this.date.getmonth();


 this.colors = {
 "cur_word"      : "#ffffff",  //当日日期文字颜色
 "cur_bg"        : "#00ff00",  //当日日期单元格背影色
 "sun_word"      : "#ff0000",  //星期天文字颜色
 "sat_word"      : "#0000ff",  //星期六文字颜色
 "td_word_light" : "#000000",  //单元格文字颜色
 "td_word_dark"  : "#cccccc",  //单元格文字暗色
 "td_bg_out"     : "#ffffff",  //单元格背影色
 "td_bg_over"    : "#ffcc00",  //单元格背影色
 "tr_word"       : "#ffffff",  //日历头文字颜色
 "tr_bg"         : "#ff6600",  //日历头背影色
 "input_border"  : "#cccccc",  //input控件的边框颜色
 "input_bg"      : "#efefef"   //input控件的背影色
 }

 this.draw();
 this.bindyear();
 this.bindmonth();
 this.changeselect();
 this.binddata();
}

/**
* 日历类属性(语言包,可自由扩展)
*/
calendar.language = {
 "year"   : [[""], [""]],
 "months" : [["一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"],
        ["jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec"]
       ],
 "weeks"  : [["日","一","二","三","四","五","六"],
        ["sun","mon","tur","wed","thu","fri","sat"]
       ],
 "clear"  : [["清空"], ["cls"]],
 "today"  : [["今天"], ["today"]],
 "close"  : [["关闭"], ["close"]]
}

calendar.prototype.draw = function() {
 calendar = this;

 var mvary = [];
 mvary[mvary.length]  = '  <form name="calendarform" style="margin: 0px;">';
 mvary[mvary.length]  = '    <table width="100%" border="0" cellpadding="0" cellspacing="0">';
 mvary[mvary.length]  = '      <tr>';
 mvary[mvary.length]  = '        <th align="left" width="1%"><input style="border: 1px solid ' + calendar.colors["input_border"] + ';background-color:' + calendar.colors["input_bg"] + ';width:16px;height:20px;" name="prevmonth" type="button" id="prevmonth" value="<" /></th>';
 mvary[mvary.length]  = '        <th align="center" width="98%" nowrap="nowrap"><select name="calendaryear" id="calendaryear" style="font-size:12px;width:50%;"></select><select name="calendarmonth" id="calendarmonth" style="font-size:12px;width:50%;"></select></th>';
 mvary[mvary.length]  = '        <th align="right" width="1%"><input style="border: 1px solid ' + calendar.colors["input_border"] + ';background-color:' + calendar.colors["input_bg"] + ';width:16px;height:20px;" name="nextmonth" type="button" id="nextmonth" value=">" /></th>';
 mvary[mvary.length]  = '      </tr>';
 mvary[mvary.length]  = '    </table>';
 mvary[mvary.length]  = '    <table id="calendartable" width="100%" style="border:0px solid #cccccc;background-color:#ddd" border="0" cellpadding="3" cellspacing="1">';
 mvary[mvary.length]  = '      <tr>';
 for(var i = 0; i < 7; i++) {
   mvary[mvary.length]  = '      <th style="font-weight:normal;background-color:' + calendar.colors["tr_bg"] + ';color:' + calendar.colors["tr_word"] + ';">' + calendar.language["weeks"][this.lang][i] + '</th>';
 }
 mvary[mvary.length]  = '      </tr>';
 for(var i = 0; i < 6;i++){
   mvary[mvary.length]  = '    <tr align="center">';
   for(var j = 0; j < 7; j++) {
     if (j == 0){
       mvary[mvary.length]  = '  <td style="cursor:default;color:' + calendar.colors["sun_word"] + ';"></td>';
     } else if(j == 6) {
       mvary[mvary.length]  = '  <td style="cursor:default;color:' + calendar.colors["sat_word"] + ';"></td>';
     } else {
       mvary[mvary.length]  = '  <td style="cursor:default;"></td>';
     }
   }
   mvary[mvary.length]  = '    </tr>';
 }
 mvary[mvary.length]  = '      <tr style="background-color:' + calendar.colors["input_bg"] + ';">';
 mvary[mvary.length]  = '        <th colspan="2"><input name="calendarclear" type="button" id="calendarclear" value="' + calendar.language["clear"][this.lang] + '" style="border: 1px solid ' + calendar.colors["input_border"] + ';background-color:' + calendar.colors["input_bg"] + ';width:100%;height:20px;font-size:12px;"/></th>';
 mvary[mvary.length]  = '        <th colspan="3"><input name="calendartoday" type="button" id="calendartoday" value="' + calendar.language["today"][this.lang] + '" style="border: 1px solid ' + calendar.colors["input_border"] + ';background-color:' + calendar.colors["input_bg"] + ';width:100%;height:20px;font-size:12px;"/></th>';
 mvary[mvary.length]  = '        <th colspan="2"><input name="calendarclose" type="button" id="calendarclose" value="' + calendar.language["close"][this.lang] + '" style="border: 1px solid ' + calendar.colors["input_border"] + ';background-color:' + calendar.colors["input_bg"] + ';width:100%;height:20px;font-size:12px;"/></th>';
 mvary[mvary.length]  = '      </tr>';
 mvary[mvary.length]  = '    </table>';
 mvary[mvary.length]  = '  </form>';
 this.panel.innerhtml = mvary.join("");
 this.form = document.forms["calendarform"];

 this.form.prevmonth.onclick = function () {calendar.goprevmonth(this);}
 this.form.nextmonth.onclick = function () {calendar.gonextmonth(this);}

 this.form.calendarclear.onclick = function () {calendar.datecontrol.value = "";calendar.hide();}
 this.form.calendarclose.onclick = function () {calendar.hide();}
 this.form.calendaryear.onchange = function () {calendar.update(this);}
 this.form.calendarmonth.onchange = function () {calendar.update(this);}
 this.form.calendartoday.onclick = function () {
   var today = new date();
   calendar.date = today;
   calendar.year = today.getfullyear();
   calendar.month = today.getmonth();
   calendar.changeselect();
   calendar.binddata();
   calendar.datecontrol.value = today.format(calendar.dateformatstyle);
   calendar.hide();
 }

}

//年份下拉框绑定数据
calendar.prototype.bindyear = function() {
 var cy = this.form.calendaryear;
 cy.length = 0;
 for (var i = this.beginyear; i <= this.endyear; i++){
   cy.options[cy.length] = new option(i + calendar.language["year"][this.lang], i);
 }
}

//月份下拉框绑定数据
calendar.prototype.bindmonth = function() {
 var cm = this.form.calendarmonth;
 cm.length = 0;
 for (var i = 0; i < 12; i++){
   cm.options[cm.length] = new option(calendar.language["months"][this.lang][i], i);
 }
}

//向前一月
calendar.prototype.goprevmonth = function(e){
 if (this.year == this.beginyear && this.month == 0){return;}
 this.month--;
 if (this.month == -1) {
   this.year--;
   this.month = 11;
 }
 this.date = new date(this.year, this.month, 1);
 this.changeselect();
 this.binddata();
}

//向后一月
calendar.prototype.gonextmonth = function(e){
 if (this.year == this.endyear && this.month == 11){return;}
 this.month++;
 if (this.month == 12) {
   this.year++;
   this.month = 0;
 }
 this.date = new date(this.year, this.month, 1);
 this.changeselect();
 this.binddata();
}

//改变select选中状态
calendar.prototype.changeselect = function() {
 var cy = this.form.calendaryear;
 var cm = this.form.calendarmonth;
 for (var i= 0; i < cy.length; i++){
   if (cy.options[i].value == this.date.getfullyear()){
     cy[i].selected = true;
     break;
   }
 }
 for (var i= 0; i < cm.length; i++){
   if (cm.options[i].value == this.date.getmonth()){
     cm[i].selected = true;
     break;
   }
 }
}

//更新年、月
calendar.prototype.update = function (e){
 this.year  = e.form.calendaryear.options[e.form.calendaryear.selectedindex].value;
 this.month = e.form.calendarmonth.options[e.form.calendarmonth.selectedindex].value;
 this.date = new date(this.year, this.month, 1);
 this.changeselect();
 this.binddata();
}

//绑定数据到月视图
calendar.prototype.binddata = function () {
 var calendar = this;
 var datearray = this.getmonthviewarray(this.date.getyear(), this.date.getmonth());
 var tds = this.getelementbyid("calendartable").getelementsbytagname("td");
 for(var i = 0; i < tds.length; i++) {
 //tds[i].style.color = calendar.colors["td_word_light"];
 tds[i].style.backgroundcolor = calendar.colors["td_bg_out"];
   tds[i].onclick = function () {return;}
   tds[i].onmouseover = function () {return;}
   tds[i].onmouseout = function () {return;}
   if (i > datearray.length - 1) break;
   tds[i].innerhtml = datearray[i];
   if (datearray[i] != " "){
     tds[i].onclick = function () {
       if (calendar.datecontrol != null){
         calendar.datecontrol.value = new date(calendar.date.getfullyear(),
                                               calendar.date.getmonth(),
                                               this.innerhtml).format(calendar.dateformatstyle);
       }
       calendar.hide();
     }
     tds[i].onmouseover = function () {
       this.style.backgroundcolor = calendar.colors["td_bg_over"];
     }
     tds[i].onmouseout = function () {
       this.style.backgroundcolor = calendar.colors["td_bg_out"];
     }
     if (new date().format(calendar.dateformatstyle) ==
         new date(calendar.date.getfullyear(),
                  calendar.date.getmonth(),
                  datearray[i]).format(calendar.dateformatstyle)) {
       //tds[i].style.color = calendar.colors["cur_word"];
       tds[i].style.backgroundcolor = calendar.colors["cur_bg"];
       tds[i].onmouseover = function () {
         this.style.backgroundcolor = calendar.colors["td_bg_over"];
       }
       tds[i].onmouseout = function () {
         this.style.backgroundcolor = calendar.colors["cur_bg"];
       }
     }//end if
   }
 }
}

//根据年、月得到月视图数据(数组形式)
calendar.prototype.getmonthviewarray = function (y, m) {
 var mvarray = [];
 var dayoffirstday = new date(y, m, 1).getday();
 var daysofmonth = new date(y, m + 1, 0).getdate();
 for (var i = 0; i < 42; i++) {
   mvarray[i] = " ";
 }
 for (var i = 0; i < daysofmonth; i++){
   mvarray[i + dayoffirstday] = i + 1;
 }
 return mvarray;
}

//扩展 document.getelementbyid(id) 多浏览器兼容性
calendar.prototype.getelementbyid = function(id){
 if (typeof(id) != "string" || id == "") return null;
 if (document.getelementbyid) return document.getelementbyid(id);
 if (document.all) return document.all(id);
 try {return eval(id);} catch(e){ return null;}
}

//扩展 object.getelementsbytagname(tagname)
calendar.prototype.getelementsbytagname = function(object, tagname){
 if (document.getelementsbytagname) return document.getelementsbytagname(tagname);
 if (document.all) return document.all.tags(tagname);
}

//取得html控件绝对位置
calendar.prototype.getabspoint = function (e){
 var x = e.offsetleft;
 var y = e.offsettop;
 while(e = e.offsetparent){
   x += e.offsetleft;
   y += e.offsettop;
 }
 return {"x": x, "y": y};
}

//显示日历
calendar.prototype.show = function (datecontrol, popcontrol) {
 if (datecontrol == null){
   throw new error("arguments[0] is necessary")
 }
 this.datecontrol = datecontrol;
 if (datecontrol.value.length > 0){
 this.date = new date(datecontrol.value.todate());
 this.year = this.date.getfullyear();
 this.month = this.date.getmonth();
   this.changeselect();
   this.binddata();
 }
 if (popcontrol == null){
   popcontrol = datecontrol;
 }
 var xy = this.getabspoint(popcontrol);
 this.panel.parentnode.style.left = xy.x + "px";
 this.panel.parentnode.style.top = (xy.y + datecontrol.offsetheight) + "px";
 this.panel.parentnode.style.visibility = "visible";
}

//隐藏日历
calendar.prototype.hide = function() {
 this.panel.parentnode.style.visibility = "hidden";
}


var html = '<div style="\
  position:absolute;visibility:hidden;z-index:9999;background-color:#fff;border:2px solid #ccc;width:225px;font-size:12px;\
  "><iframe style="position:absolute;width:100%;height:199px;z-index:-1;border:none"></iframe>\
  <div id="calendarpanel"></div>\
  </div>';
document.write(html);

</script>
</head>
<body>
<!--
//英文:
new calendar(1).show(this)
-->
<input class=textbox onclick="new calendar(0).show(this)" 
readonly size="23" value="2007-03-16" name=posttime>
</body>
</html>

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网