日历JS代码
发布日期:2021-05-09 05:03:14
浏览次数:11
分类:博客文章
本文共 20178 字,大约阅读时间需要 67 分钟。
转自:
略加修改
Code 1 2 var cal; 3 var isFocus=false; //是否为焦点 4 //以上为 寒羽枫 2006-06-25 添加的变量 5 6 //选择日期 → 由 寒羽枫 2006-06-25 添加 7 function SelectDate(obj,strFormat) 8 { 9 var date = new Date(); 10 var by = date.getFullYear()-50; //最小值 → 50 年前 11 var ey = date.getFullYear()+50; //最大值 → 50 年后 12 //cal = new Calendar(by, ey,1,strFormat); //初始化英文版,0 为中文版 13 cal = (cal==null) ? new Calendar(by, ey, 0) : cal; //不用每次都初始化 2006-12-03 修正 14 cal.dateFormatStyle = strFormat; 15 cal.show(obj); 16 } 17 /**//**//**//** 18 * 返回日期 19 * @param d the delimiter 20 * @param p the pattern of your date 21 2006-06-25 由 寒羽枫 修改为根据用户指定的 style 来确定; 22 */ 23 //String.prototype.toDate = function(x, p) { 24 String.prototype.toDate = function(style) { 25 /**//**//**//* 26 if(x == null) x = "-"; 27 if(p == null) p = "ymd"; 28 var a = this.split(x); 29 var y = parseInt(a[p.indexOf("y")]); 30 //remember to change this next century ;) 31 if(y.toString().length <= 2) y += 2000; 32 if(isNaN(y)) y = new Date().getFullYear(); 33 var m = parseInt(a[p.indexOf("m")]) - 1; 34 var d = parseInt(a[p.indexOf("d")]); 35 if(isNaN(d)) d = 1; 36 return new Date(y, m, d); 37 */ 38 var y = this.substring(style.indexOf('y'),style.lastIndexOf('y')+1);//年 39 var m = this.substring(style.indexOf('M'),style.lastIndexOf('M')+1);//月 40 var d = this.substring(style.indexOf('d'),style.lastIndexOf('d')+1);//日 41 if(isNaN(y)) y = new Date().getFullYear(); 42 if(isNaN(m)) m = new Date().getMonth(); 43 if(isNaN(d)) d = new Date().getDate(); 44 var dt ; 45 eval ("dt = new Date('"+ y+"', '"+(m-1)+"','"+ d +"')"); 46 return dt; 47 } 48 49 /**//**//**//** 50 * 格式化日期 51 * @param d the delimiter 52 * @param p the pattern of your date 53 * @author meizz 54 */ 55 Date.prototype.format = function(style) { 56 var o = { 57 "M+" : this.getMonth() + 1, //month 58 "d+" : this.getDate(), //day 59 "h+" : this.getHours(), //hour 60 "m+" : this.getMinutes(), //minute 61 "s+" : this.getSeconds(), //second 62 "w+" : "天一二三四五六".charAt(this.getDay()), //week 63 "q+" : Math.floor((this.getMonth() + 3) / 3), //quarter 64 "S" : this.getMilliseconds() //millisecond 65 } 66 if(/(y+)/.test(style)) { 67 style = style.replace(RegExp.$1, 68 (this.getFullYear() + "").substr(4 - RegExp.$1.length)); 69 } 70 for(var k in o) { 71 if(new RegExp("("+ k +")").test(style)) { 72 style = style.replace(RegExp.$1, 73 RegExp.$1.length == 1 ? o[k] : 74 ("00" + o[k]).substr(("" + o[k]).length)); 75 } 76 } 77 return style; 78 }; 79 80 /**//**//**//** 81 * 日历类 82 * @param beginYear 1990 83 * @param endYear 2010 84 * @param lang 0(中文)|1(英语) 可自由扩充 85 * @param dateFormatStyle "yyyy-MM-dd"; 86 * @version 2006-04-01 87 * @author KimSoft (jinqinghua [at] gmail.com) 88 * @update 89 */ 90 function Calendar(beginYear, endYear, lang, dateFormatStyle) { 91 this.beginYear = 1990; 92 this.endYear = 2020; 93 this.lang = 1; //0(中文) | 1(英文) 94 this.dateFormatStyle = "yyyy-MM-dd"; 95 96 if (beginYear != null && endYear != null) { 97 this.beginYear = beginYear; 98 this.endYear = endYear; 99 }100 if (lang != null) {101 this.lang = lang102 }103 104 if (dateFormatStyle != null) {105 this.dateFormatStyle = dateFormatStyle106 }107 108 this.dateControl = null;109 this.panel = this.getElementById("calendarPanel");110 this.container = this.getElementById("ContainerPanel");111 this.form = null;112 113 this.date = new Date();114 this.year = this.date.getFullYear();115 this.month = this.date.getMonth();116 117 118 this.colors = {119 "cur_word" : "#FFFFFF", //当日日期文字颜色120 "cur_bg" : "#00FF00", //当日日期单元格背影色121 "sel_bg" : "#FFCCCC", //已被选择的日期单元格背影色 2006-12-03 寒羽枫添加122 "sun_word" : "#FF0000", //星期天文字颜色123 "sat_word" : "#0000FF", //星期六文字颜色124 "td_word_light" : "#333333", //单元格文字颜色125 "td_word_dark" : "#CCCCCC", //单元格文字暗色126 "td_bg_out" : "#EFEFEF", //单元格背影色127 "td_bg_over" : "#FFCC00", //单元格背影色128 "tr_word" : "#FFFFFF", //日历头文字颜色129 "tr_bg" : "#666666", //日历头背影色130 "input_border" : "#CCCCCC", //input控件的边框颜色131 "input_bg" : "#EFEFEF" //input控件的背影色132 }133 134 this.draw();135 this.bindYear();136 this.bindMonth();137 this.changeSelect();138 this.bindData();139 }140 141 /**//**//**//**142 * 日历类属性(语言包,可自由扩展)143 */144 Calendar.language = {145 "year" : [[""], [""]],146 "months" : [["一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"],147 ["JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"]148 ],149 "weeks" : [["日","一","二","三","四","五","六"],150 ["SUN","MON","TUR","WED","THU","FRI","SAT"]151 ],152 "clear" : [["清空"], ["CLS"]],153 "today" : [["今天"], ["TODAY"]],154 "close" : [["关闭"], ["CLOSE"]]155 }156 157 Calendar.prototype.draw = function() {158 calendar = this;159 160 var mvAry = [];161 //mvAry[mvAry.length] = ' <form name="calendarForm" style="margin: 0px;">'; //因 <form> 不能嵌套, 2006-12-01 由寒羽枫改用 Div162 mvAry[mvAry.length] = ' <div name="calendarForm" style="margin: 0px;">';163 mvAry[mvAry.length] = ' <table width="100%" border="0" cellpadding="0" cellspacing="1">';164 mvAry[mvAry.length] = ' <tr>';165 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>';166 mvAry[mvAry.length] = ' <th align="center" width="98%" nowrap="nowrap"><select name="calendarYear" id="calendarYear" style="font-size:12px;"></select><select name="calendarMonth" id="calendarMonth" style="font-size:12px;"></select></th>';167 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>';168 mvAry[mvAry.length] = ' </tr>';169 mvAry[mvAry.length] = ' </table>';170 mvAry[mvAry.length] = ' <table id="calendarTable" width="100%" style="border:0px solid #CCCCCC;background-color:#FFFFFF" border="0" cellpadding="3" cellspacing="1">';171 mvAry[mvAry.length] = ' <tr>';172 for(var i = 0; i < 7; i++) {173 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>';174 }175 mvAry[mvAry.length] = ' </tr>';176 for(var i = 0; i < 6;i++) {177 mvAry[mvAry.length] = ' <tr align="center">';178 for(var j = 0; j < 7; j++) {179 if (j == 0) {180 mvAry[mvAry.length] = ' <td style="cursor:default;color:' + calendar.colors["sun_word"] + ';"></td>';181 } else if(j == 6) {182 mvAry[mvAry.length] = ' <td style="cursor:default;color:' + calendar.colors["sat_word"] + ';"></td>';183 } else {184 mvAry[mvAry.length] = ' <td style="cursor:default;"></td>';185 }186 }187 mvAry[mvAry.length] = ' </tr>';188 }189 mvAry[mvAry.length] = ' <tr style="background-color:' + calendar.colors["input_bg"] + ';">';190 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>';191 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>';192 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>';193 mvAry[mvAry.length] = ' </tr>';194 mvAry[mvAry.length] = ' </table>';195 //mvAry[mvAry.length] = ' </from>';196 mvAry[mvAry.length] = ' </div>';197 this.panel.innerHTML = mvAry.join("");198 199 /**//******** 以下代码由寒羽枫 2006-12-01 添加 **********/200 var obj = this.getElementById("prevMonth");201 obj.onclick = function () {calendar.goPrevMonth(calendar);}202 obj.onblur = function () {calendar.onblur();}203 this.prevMonth= obj;204 205 obj = this.getElementById("nextMonth");206 obj.onclick = function () {calendar.goNextMonth(calendar);}207 obj.onblur = function () {calendar.onblur();}208 this.nextMonth= obj;209 210 obj = this.getElementById("calendarClear");211 obj.onclick = function () {calendar.dateControl.value = "";calendar.hide();}212 this.calendarClear = obj;213 214 obj = this.getElementById("calendarClose");215 obj.onclick = function () {calendar.hide();}216 this.calendarClose = obj;217 218 obj = this.getElementById("calendarYear");219 obj.onchange = function () {calendar.update(calendar);}220 obj.onblur = function () {calendar.onblur();}221 this.calendarYear = obj;222 223 obj = this.getElementById("calendarMonth");224 with(obj)225 {226 onchange = function () {calendar.update(calendar);}227 onblur = function () {calendar.onblur();}228 }this.calendarMonth = obj;229 230 obj = this.getElementById("calendarToday");231 obj.onclick = function () {232 var today = new Date();233 calendar.date = today;234 calendar.year = today.getFullYear();235 calendar.month = today.getMonth();236 calendar.changeSelect();237 calendar.bindData();238 calendar.dateControl.value = today.format(calendar.dateFormatStyle);239 calendar.hide();240 }241 this.calendarToday = obj;242 /**//******** 以上代码由寒羽枫 2006-12-01 添加 **********/243 244 /**//*245 //this.form = document.forms["calendarForm"]; 246 this.form.prevMonth.onclick = function () {calendar.goPrevMonth(this);}247 this.form.nextMonth.onclick = function () {calendar.goNextMonth(this);}248 249 this.form.prevMonth.onblur = function () {calendar.onblur();}250 this.form.nextMonth.onblur = function () {calendar.onblur();}251 252 this.form.calendarClear.onclick = function () {calendar.dateControl.value = "";calendar.hide();}253 this.form.calendarClose.onclick = function () {calendar.hide();}254 this.form.calendarYear.onchange = function () {calendar.update(this);}255 this.form.calendarMonth.onchange = function () {calendar.update(this);}256 257 this.form.calendarYear.onblur = function () {calendar.onblur();}258 this.form.calendarMonth.onblur = function () {calendar.onblur();}259 260 this.form.calendarToday.onclick = function () {261 var today = new Date();262 calendar.date = today;263 calendar.year = today.getFullYear();264 calendar.month = today.getMonth();265 calendar.changeSelect();266 calendar.bindData();267 calendar.dateControl.value = today.format(calendar.dateFormatStyle);268 calendar.hide();269 }270 */271 }272 273 //年份下拉框绑定数据274 Calendar.prototype.bindYear = function() {275 //var cy = this.form.calendarYear;276 var cy = this.calendarYear;//2006-12-01 由寒羽枫修改277 cy.length = 0;278 for (var i = this.beginYear; i <= this.endYear; i++) {279 cy.options[cy.length] = new Option(i + Calendar.language["year"][this.lang], i);280 }281 }282 283 //月份下拉框绑定数据284 Calendar.prototype.bindMonth = function() {285 //var cm = this.form.calendarMonth;286 var cm = this.calendarMonth;//2006-12-01 由寒羽枫修改287 cm.length = 0;288 for (var i = 0; i < 12; i++) {289 cm.options[cm.length] = new Option(Calendar.language["months"][this.lang][i], i);290 }291 }292 293 //向前一月294 Calendar.prototype.goPrevMonth = function(e) {295 if (this.year == this.beginYear && this.month == 0) {return;}296 this.month--;297 if (this.month == -1) {298 this.year--;299 this.month = 11;300 }301 this.date = new Date(this.year, this.month, 1);302 this.changeSelect();303 this.bindData();304 }305 306 //向后一月307 Calendar.prototype.goNextMonth = function(e) {308 if (this.year == this.endYear && this.month == 11) {return;}309 this.month++;310 if (this.month == 12) {311 this.year++;312 this.month = 0;313 }314 this.date = new Date(this.year, this.month, 1);315 this.changeSelect();316 this.bindData();317 }318 319 //改变SELECT选中状态320 Calendar.prototype.changeSelect = function() {321 //var cy = this.form.calendarYear;322 //var cm = this.form.calendarMonth;323 var cy = this.calendarYear;//2006-12-01 由寒羽枫修改324 var cm = this.calendarMonth;325 for (var i= 0; i < cy.length; i++) {326 if (cy.options[i].value == this.date.getFullYear()) {327 cy[i].selected = true;328 break;329 }330 }331 for (var i= 0; i < cm.length; i++) {332 if (cm.options[i].value == this.date.getMonth()) {333 cm[i].selected = true;334 break;335 }336 }337 }338 339 //更新年、月340 Calendar.prototype.update = function (e) {341 //this.year = e.form.calendarYear.options[e.form.calendarYear.selectedIndex].value;342 //this.month = e.form.calendarMonth.options[e.form.calendarMonth.selectedIndex].value;343 this.year = e.calendarYear.options[e.calendarYear.selectedIndex].value;//2006-12-01 由寒羽枫修改344 this.month = e.calendarMonth.options[e.calendarMonth.selectedIndex].value;345 this.date = new Date(this.year, this.month, 1);346 this.changeSelect();347 this.bindData();348 }349 350 //绑定数据到月视图351 Calendar.prototype.bindData = function () {352 var calendar = this;353 var dateArray = this.getMonthViewArray(this.date.getYear(), this.date.getMonth());354 var tds = this.getElementById("calendarTable").getElementsByTagName("td");355 for(var i = 0; i < tds.length; i++) {356 //tds[i].style.color = calendar.colors["td_word_light"];357 tds[i].style.backgroundColor = calendar.colors["td_bg_out"];358 tds[i].onclick = function () {return;}359 tds[i].onmouseover = function () {return;}360 tds[i].onmouseout = function () {return;}361 if (i > dateArray.length - 1) break;362 tds[i].innerHTML = dateArray[i];363 if (dateArray[i] != " ") {364 tds[i].onclick = function () {365 if (calendar.dateControl != null) {366 calendar.dateControl.value = new Date(calendar.date.getFullYear(),367 calendar.date.getMonth(),368 this.innerHTML).format(calendar.dateFormatStyle);369 }370 calendar.hide();371 }372 tds[i].onmouseover = function () {373 this.style.backgroundColor = calendar.colors["td_bg_over"];374 }375 tds[i].onmouseout = function () {376 this.style.backgroundColor = calendar.colors["td_bg_out"];377 }378 if (new Date().format(calendar.dateFormatStyle) ==379 new Date(calendar.date.getFullYear(),380 calendar.date.getMonth(),381 dateArray[i]).format(calendar.dateFormatStyle)) {382 //tds[i].style.color = calendar.colors["cur_word"];383 tds[i].style.backgroundColor = calendar.colors["cur_bg"];384 tds[i].onmouseover = function () {385 this.style.backgroundColor = calendar.colors["td_bg_over"];386 }387 tds[i].onmouseout = function () {388 this.style.backgroundColor = calendar.colors["cur_bg"];389 }390 //continue; //若不想当天单元格的背景被下面的覆盖,请取消注释 → 2006-12-03 寒羽枫添加391 }//end if392 393 //设置已被选择的日期单元格背影色 2006-12-03 寒羽枫添加394 if (calendar.dateControl != null && calendar.dateControl.value == new Date(calendar.date.getFullYear(),395 calendar.date.getMonth(),396 dateArray[i]).format(calendar.dateFormatStyle)) {397 tds[i].style.backgroundColor = calendar.colors["sel_bg"];398 tds[i].onmouseover = function () {399 this.style.backgroundColor = calendar.colors["td_bg_over"];400 }401 tds[i].onmouseout = function () {402 this.style.backgroundColor = calendar.colors["sel_bg"];403 }404 }405 }406 }407 }408 409 //根据年、月得到月视图数据(数组形式)410 Calendar.prototype.getMonthViewArray = function (y, m) {411 var mvArray = [];412 var dayOfFirstDay = new Date(y, m, 1).getDay();413 var daysOfMonth = new Date(y, m + 1, 0).getDate();414 for (var i = 0; i < 42; i++) {415 mvArray[i] = " ";416 }417 for (var i = 0; i < daysOfMonth; i++) {418 mvArray[i + dayOfFirstDay] = i + 1;419 }420 return mvArray;421 }422 423 //扩展 document.getElementById(id) 多浏览器兼容性 from meizz tree source424 Calendar.prototype.getElementById = function(id) {425 if (typeof(id) != "string" || id == "") return null;426 if (document.getElementById) return document.getElementById(id);427 if (document.all) return document.all(id);428 try {return eval(id);} catch(e) { return null;}429 }430 431 //扩展 object.getElementsByTagName(tagName)432 Calendar.prototype.getElementsByTagName = function(object, tagName) {433 if (document.getElementsByTagName) return document.getElementsByTagName(tagName);434 if (document.all) return document.all.tags(tagName);435 }436 437 //取得HTML控件绝对位置438 Calendar.prototype.getAbsPoint = function (e) {439 var x = e.offsetLeft;440 var y = e.offsetTop;441 while(e = e.offsetParent) {442 x += e.offsetLeft;443 y += e.offsetTop;444 }445 return {"x": x, "y": y};446 }447 448 //显示日历449 Calendar.prototype.show = function (dateObj, popControl) {450 if (dateObj == null) {451 throw new Error("arguments[0] is necessary")452 }453 this.dateControl = dateObj;454 455 //if (dateObj.value.length > 0){456 //this.date = new Date(dateObj.value.toDate());457 //this.date = new Date(dateObj.value.toDate(this.dateFormatStyle));//由寒羽枫修改,带入用户指定的 style 458 this.date = (dateObj.value.length > 0) ? new Date(dateObj.value.toDate(this.dateFormatStyle)) : new Date() ;//2006-12-03 寒羽枫添加 → 若为空则显示当前月份459 this.year = this.date.getFullYear();460 this.month = this.date.getMonth();461 this.changeSelect();462 this.bindData();463 //}464 if (popControl == null) {465 popControl = dateObj;466 }467 var xy = this.getAbsPoint(popControl);468 this.panel.style.left = xy.x -25 + "px";469 this.panel.style.top = (xy.y + dateObj.offsetHeight) + "px";470 471 //由寒羽枫 2006-06-25 修改 → 把 visibility 变为 display,并添加失去焦点的事件472 //this.setDisplayStyle("select", "hidden");473 //this.panel.style.visibility = "visible";474 //this.container.style.visibility = "visible";475 this.panel.style.display = "";476 this.container.style.display = "";477 478 dateObj.onblur = function() {calendar.onblur();}479 this.container.onmouseover = function() {isFocus=true;}480 this.container.onmouseout = function() {isFocus=false;}481 }482 483 //隐藏日历484 Calendar.prototype.hide = function() {485 //this.setDisplayStyle("select", "visible");486 //this.panel.style.visibility = "hidden";487 //this.container.style.visibility = "hidden";488 this.panel.style.display = "none";489 this.container.style.display = "none";490 isFocus=false;491 }492 493 //焦点转移时隐藏日历 → 由寒羽枫 2006-06-25 添加494 Calendar.prototype.onblur = function() {495 if(!isFocus) {this.hide();}496 }497 498 //以下由寒羽枫 2006-06-25 修改 → 用<iframe> 遮住 IE 的下拉框499 /**//**//**//*500 //设置控件显示或隐藏501 Calendar.prototype.setDisplayStyle = function(tagName, style) {502 var tags = this.getElementsByTagName(null, tagName)503 for(var i = 0; i < tags.length; i++) {504 if (tagName.toLowerCase() == "select" &&505 (tags[i].name == "calendarYear" ||506 tags[i].name == "calendarMonth")){507 continue;508 }509 //tags[i].style.visibility = style;510 tags[i].style.display = style;511 }512 }513 */514 //document.write('<div id="ContainerPanel" style="visibility:hidden"><div id="calendarPanel" style="position: absolute;visibility: hidden;z-index: 9999;');515 document.write('<div id="ContainerPanel" style="display:none"><div id="calendarPanel" style="position: absolute;display: none;z-index: 9999;');516 document.write('background-color: #FFFFFF;border: 1px solid #CCCCCC;width:175px;font-size:12px;"></div>');517 if(document.all)518 {519 document.write('<iframe style="position:absolute;z-index:2000;width:expression(this.previousSibling.offsetWidth);');520 document.write('height:expression(this.previousSibling.offsetHeight);');521 document.write('left:expression(this.previousSibling.offsetLeft);top:expression(this.previousSibling.offsetTop);');522 document.write('display:expression(this.previousSibling.style.display);" scrolling="no" frameborder="no"></iframe>');523 }524 document.write('</div>');525 //var calendar = new Calendar(); //此句被 寒羽枫注释,否则 IE 将报错526 //调用calendar.show(dateControl, popControl);527 528
将上面代码存入WebCalendar.js 然后在你的控件中加入后面代码:onclick="SelectDate(this,'yyyy-MM-dd')"
发表评论
最新留言
初次前来,多多关照!
[***.217.46.12]2025年04月14日 01时10分44秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
白红宇的个人博客 - 记录点点滴滴的事 - 您是第 459905000 位访客
访问时间: 2025-04-21 09:34:44
访问IP: 13.58.173.156
Copyright © 2020 - 2025 css8.cn 京ICP备2021015314号-1
手机版
请用微信扫一扫,非常感谢您的关注!
您的支持就是我的动力!您也可以点击二维码关闭!
您的支持就是我的动力!您也可以点击二维码关闭!
