下面是js代码,保存为xx.js,页面上运用即可,当然是站在前人的肩膀上,多多包涵
View Code
下面是调用的代码 1 ///textbox的日期控件,created by pierce 2012-11-20 2 ///只需添加此js文件,并引用extjs基础文件即可 3 ///组件的格式如:new Ext.ux.form.datetextfield({ }) 4 5 var cal; 6 var isFocus = false; //是否为焦点 7 var pickMode = { 8 "second": 1, 9 "minute": 2, 10 "hour": 3, 11 "day": 4, 12 "month": 5, 13 "year": 6 14 }; 15 var topY = 0, leftX = 0; //自定义定位偏移量 2007-02-11 16 function SelectDateById(id, strFormat, x, y) { 17 var obj = document.getElementById(id); 18 if (obj == null) { return false; } 19 obj.focus(); 20 if (obj.onclick != null) { obj.onclick(); } 21 else if (obj.click != null) { obj.click(); } 22 else { SelectDate(obj, strFormat, x, y) } 23 } 24 function SelectDate(obj, strFormat, x, y, f) { 25 leftX = (x == null) ? leftX : x; 26 topY = (y == null) ? topY : y; //自定义定位偏移量 if (document.getElementById("ContainerPanel") == null) { InitContainerPanel(); } 27 var date = new Date(); 28 var by = date.getFullYear() - 50; //最小值 → 50 年前 29 var ey = date.getFullYear() + 50; //最大值 → 50 年后 30 //cal = new Calendar(by, ey,1,strFormat); //初始化英文版,0 为中文版 31 cal = (cal == null) ? new Calendar(by, ey, 0) : cal; //不用每次都初始化 2006-12-03 修正 32 cal.DateMode = pickMode["second"]; //复位 33 if (strFormat.indexOf('s') < 0) { cal.DateMode = pickMode["minute"]; } //精度为分 34 if (strFormat.indexOf('m') < 0) { cal.DateMode = pickMode["hour"]; } //精度为时 35 if (strFormat.indexOf('h') < 0) { cal.DateMode = pickMode["day"]; } //精度为日 36 if (strFormat.indexOf('d') < 0) { cal.DateMode = pickMode["month"]; } //精度为月 37 if (strFormat.indexOf('M') < 0) { cal.DateMode = pickMode["year"]; } //精度为年 38 if (strFormat.indexOf('y') < 0) { cal.DateMode = pickMode["second"]; } //默认精度为秒 39 cal.dateFormatStyleOld = cal.dateFormatStyle; 40 cal.dateFormatStyle = strFormat; 41 cal.show(obj, null, f); 42 } 43 String.prototype.toDate = function (style) { 44 var y = this.substring(style.indexOf('y'), style.lastIndexOf('y') + 1); //年 45 var M = this.substring(style.indexOf('M'), style.lastIndexOf('M') + 1); //月 46 var d = this.substring(style.indexOf('d'), style.lastIndexOf('d') + 1); //日 47 var h = this.substring(style.indexOf('h'), style.lastIndexOf('h') + 1); //时 48 var m = this.substring(style.indexOf('m'), style.lastIndexOf('m') + 1); //分 49 var s = this.substring(style.indexOf('s'), style.lastIndexOf('s') + 1); //秒 50 if (s == null || s == "" || isNaN(s)) { s = new Date().getSeconds(); } 51 if (m == null || m == "" || isNaN(m)) { m = new Date().getMinutes(); } 52 if (h == null || h == "" || isNaN(h)) { h = new Date().getHours(); } 53 if (d == null || d == "" || isNaN(d)) { d = new Date().getDate(); } 54 if (M == null || M == "" || isNaN(M)) { M = new Date().getMonth() + 1; } 55 if (y == null || y == "" || isNaN(y)) { y = new Date().getFullYear(); } 56 var dt; 57 eval("dt = new Date('" + y + "', '" + (M - 1) + "','" + d + "','" + h + "','" + m + "','" + s + "')"); 58 return dt; 59 } 60 /**//**//**//**//**//**//**//** 61 * 格式化日期 62 * @param d the delimiter 63 * @param p the pattern of your date 64 * @author meizz 65 */ 66 Date.prototype.format = function (style) { 67 var o = { 68 "M+": this.getMonth() + 1, //month 69 "d+": this.getDate(), //day 70 "h+": this.getHours(), //hour 71 "m+": this.getMinutes(), //minute 72 "s+": this.getSeconds(), //second 73 "w+": "天一二三四五六".charAt(this.getDay()), //week 74 "q+": Math.floor((this.getMonth() + 3) / 3), //quarter 75 "S": this.getMilliseconds() //millisecond 76 } 77 if (/(y+)/.test(style)) { 78 style = style.replace(RegExp.$1, 79 (this.getFullYear() + "").substr(4 - RegExp.$1.length)); 80 } 81 for (var k in o) { 82 if (new RegExp("(" + k + ")").test(style)) { 83 style = style.replace(RegExp.$1, 84 RegExp.$1.length == 1 ? o[k] : 85 ("00" + o[k]).substr(("" + o[k]).length)); 86 } 87 } 88 return style; 89 } 90 Calendar.prototype.ReturnDate = function (dt) { 91 if (this.dateControl != null) { this.dateControl.value = dt; } 92 calendar.hide(); 93 if (this.dateControl.onchange == null) { return; } 94 //将 onchange 转成其它函数,以免触发验证事件 95 var ev = this.dateControl.onchange.toString(); //找出函数的字串 96 ev = ev.substring( 97 ((ev.indexOf("ValidatorOnChange();") > 0) ? ev.indexOf("ValidatorOnChange();") + 20 : ev.indexOf("{") + 1) 98 , ev.lastIndexOf("}")); //去除验证函数 ValidatorOnChange(); 99 var fun = new Function(ev); //重新定义函数 100 this.dateControl.changeEvent = fun;101 this.dateControl.changeEvent(); //触发自定义 changeEvent 函数 102 }103 /**//**//**//**//**//**//**//** 104 * 日历类 105 * @param beginYear 1990 106 * @param endYear 2010 107 * @param lang 0(中文)|1(英语) 可自由扩充 108 * @param dateFormatStyle "yyyy-MM-dd"; 109 * @version 2006-04-01 110 * @author KimSoft (jinqinghua [at] gmail.com) 111 * @update 112 */113 function Calendar(beginYear, endYear, lang, dateFormatStyle) {114 this.beginYear = 1950;115 this.endYear = 2050;116 this.lang = 0; //0(中文) | 1(英文) 117 this.dateFormatStyle = "yyyy-MM-dd hh:mm:ss";118 if (beginYear != null && endYear != null) {119 this.beginYear = beginYear;120 this.endYear = endYear;121 }122 if (lang != null) {123 this.lang = lang124 }125 if (dateFormatStyle != null) {126 this.dateFormatStyle = dateFormatStyle127 }128 this.dateControl = null;129 this.panel = this.getElementById("calendarPanel");130 this.container = this.getElementById("ContainerPanel");131 this.form = null;132 this.date = new Date();133 this.year = this.date.getFullYear();134 this.month = this.date.getMonth();135 this.day = this.date.getDate();136 this.hour = this.date.getHours();137 this.minute = this.date.getMinutes();138 this.second = this.date.getSeconds();139 this.colors = {140 "cur_word": "#FFFFFF", //当日日期文字颜色 141 "cur_bg": "#00FF00", //当日日期单元格背影色 142 "sel_bg": "#FFCCCC", //已被选择的日期单元格背影色 2006-12-03 寒羽枫添加 143 "sun_word": "#FF0000", //星期天文字颜色 144 "sat_word": "#0000FF", //星期六文字颜色 145 "td_word_light": "#333333", //单元格文字颜色 146 "td_word_dark": "#CCCCCC", //单元格文字暗色 147 "td_bg_out": "#EFEFEF", //单元格背影色 148 "td_bg_over": "#FFCC00", //单元格背影色 149 "tr_word": "#FFFFFF", //日历头文字颜色 150 "tr_bg": "#666666", //日历头背影色 151 "input_border": "#CCCCCC", //input控件的边框颜色 152 "input_bg": "#EFEFEF" //input控件的背影色 153 }154 /* //2008-01-29 放到了 show ,因为要做 pickMode 判断 155 this.draw(); 156 this.bindYear(); 157 this.bindMonth(); 158 */159 //this.changeSelect(); 160 //this.bindData(); }161 /**//**//**//**//**//**//**//** 162 * 日历类属性(语言包,可自由扩展) 163 */164 Calendar.language = {165 "year": [[""], [""]],166 "months": [["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"],167 ["JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"]168 ],169 "weeks": [["日", "一", "二", "三", "四", "五", "六"],170 ["SUN", "MON", "TUR", "WED", "THU", "FRI", "SAT"]171 ],172 "hour": [["时"], ["H"]],173 "minute": [["分"], ["M"]],174 "second": [["秒"], ["S"]],175 "clear": [["清空"], ["CLS"]],176 "today": [["今天"], ["TODAY"]],177 "pickTxt": [["确定"], ["OK"]], //pickMode 精确到年、月时把今天变成“确定” 178 "close": [["关闭"], ["CLOSE"]]179 }180 Calendar.prototype.draw = function () {181 calendar = this;182 var mvAry = [];183 //mvAry[mvAry.length] = '
1 items: new Ext.ux.form.datetextfield({2 xtype: 'textfield',3 width: 200,4 format: 'yyyy-MM-dd',5 fieldLabel: '时间2'6 })
运行效果如下