在 input 上选择日期时间的JS

  1. //  主调用函数是 setday(this,[object])和setday(this),[object]是控件输出的控件名,举两个例子:
  2. //  一、<input name=txt><input type=button value=setday οnclick="setday(this,document.all.txt)">
  3. //  二、<input οnfοcus="setday(this)">
  4. //function setaa()
  5. //{
  6.     //alert('1111');
  7. //}
  8. var bMoveable=true;
  9. var strFrame;       
  10. document.writeln('<iframe id=endDateLayer frameborder=0 width=162 height=211 style="position: absolute;  z-index: 9998; display: none"></iframe>');
  11. strFrame='<style>';
  12. strFrame+='INPUT.button{BORDER-RIGHT: #63A3E9 1px solid;BORDER-TOP: #63A3E9 1px solid;BORDER-LEFT: #63A3E9 1px solid;';
  13. strFrame+='BORDER-BOTTOM: #63A3E9 1px solid;BACKGROUND-COLOR: #63A3E9;font-family:宋体;}';
  14. strFrame+='TD{FONT-SIZE: 9pt;font-family:宋体;}';
  15. strFrame+='</style>';
  16. strFrame+='<scr' + 'ipt>';
  17. strFrame+='var datelayerx,datelayery;';
  18. strFrame+='var bDrag;';
  19. strFrame+='function document.onmousemove()';
  20. strFrame+='{if(bDrag && window.event.button==1)';
  21. strFrame+=' {var DateLayer=parent.document.all.endDateLayer.style;';
  22. strFrame+='     DateLayer.posLeft += window.event.clientX-datelayerx;';
  23. strFrame+='     DateLayer.posTop += window.event.clientY-datelayery;}}';
  24. strFrame+='function DragStart()';
  25. strFrame+='{var DateLayer=parent.document.all.endDateLayer.style;';
  26. strFrame+=' datelayerx=window.event.clientX;';
  27. strFrame+=' datelayery=window.event.clientY;';
  28. strFrame+=' bDrag=true;}';
  29. strFrame+='function DragEnd(){';
  30. strFrame+=' bDrag=false;}';
  31. strFrame+='</scr' + 'ipt>';
  32. strFrame+='<div style="z-index:9999;position: absolute; left:0; top:0;" onselectstart="return false">';
  33. strFrame+='<span id=tmpSelectYearLayer  style="z-index: 9999;position: absolute;top: 3; left: 19;display: none"></span>';
  34. strFrame+='<span id=tmpSelectMonthLayer  style="z-index: 9999;position: absolute;top: 3; left: 78;display: none"></span>';
  35. strFrame+='<span id=tmpSelectHourLayer  style="z-index: 9999;position: absolute;top: 188; left: 35px;display: none"></span>';
  36. strFrame+='<span id=tmpSelectMinuteLayer style="z-index:9999;position:absolute;top: 188; left: 77px;display: none"></span>';
  37. strFrame+='<span id=tmpSelectSecondLayer style="z-index:9999;position:absolute;top: 188; left: 119px;display: none"></span>';
  38. strFrame+='<table border=1 cellspacing=0 cellpadding=0 width=142 height=160 bordercolor=#63A3E9 bgcolor=#63A3E9 >';
  39. strFrame+='    <tr><td width=142 height=23  bgcolor=#FFFFFF>';
  40. strFrame+='        <table border=0 cellspacing=1 cellpadding=0 width=158  height=23>';
  41. strFrame+='            <tr align=center >';
  42. strFrame+='                <td width=16 align=center bgcolor=#63A3E9 style="font-size:12px;cursor: hand;color: #ffffff" ';
  43. strFrame+='                    οnclick="parent.meizzPrevM()" title="向前翻 1 月" ><b ><</b></td>';
  44. strFrame+='                <td width=60 align="center" bgcolor="#63A3E9"  style="font-size:12px;cursor:hand" ';
  45. strFrame+='                    οnmοuseοver="style.backgroundColor=/'#aaccf3/'"';
  46. strFrame+='                    οnmοuseοut="style.backgroundColor=/'#63A3E9/'" ';
  47. strFrame+='                    οnclick="parent.tmpSelectYearInnerHTML(this.innerText.substring(0,4))" ';
  48. strFrame+='                    title="点击这里选择年份"><span  id=meizzYearHead></span></td>';
  49. strFrame+='                <td width=48 align="center" style="font-size:12px;font-color: #ffffff;cursor:hand" ';
  50. strFrame+='                    bgcolor="#63A3E9" οnmοuseοver="style.backgroundColor=/'#aaccf3/'" ';
  51. strFrame+='                    οnmοuseοut="style.backgroundColor=/'#63A3E9/'" ';
  52. strFrame+='                    οnclick="parent.tmpSelectMonthInnerHTML(this.innerText.length==3?this.innerText.substring(0,1):this.innerText.substring(0,2))"';
  53. strFrame+='                    title="点击这里选择月份"><span id=meizzMonthHead ></span></td>';
  54. strFrame+='                <td width=16 bgcolor=#63A3E9 align=center style="font-size:12px;cursor: hand;color: #ffffff" ';
  55. strFrame+='                    οnclick="parent.meizzNextM()" title="向后翻 1 月" ><b >></b></td>';
  56. strFrame+='            </tr>';
  57. strFrame+='        </table></td></tr>';
  58. strFrame+='    <tr><td width=142 height=18 >';
  59. strFrame+='        <table border=0 cellspacing=0 cellpadding=2 bgcolor=#63A3E9 ' + (bMoveable? 'οnmοusedοwn="DragStart()" οnmοuseup="DragEnd()"':'');
  60. strFrame+='             BORDERCOLORLIGHT=#63A3E9 BORDERCOLORDARK=#FFFFFF width=140 height=20  style="cursor:' + (bMoveable ? 'move':'default') + '">';
  61. strFrame+='    <tr><td style="font-size:12px;color:#ffffff" width=20> 日</td>';
  62. strFrame+='<td style="font-size:12px;color:#FFFFFF" > 一</td><td style="font-size:12px;color:#FFFFFF"> 二</td>';
  63. strFrame+='<td style="font-size:12px;color:#FFFFFF" > 三</td><td style="font-size:12px;color:#FFFFFF" > 四</td>';
  64. strFrame+='<td style="font-size:12px;color:#FFFFFF" > 五</td><td style="font-size:12px;color:#FFFFFF" > 六</td></tr>';
  65. strFrame+='</table></td></tr>';
  66. strFrame+='  <tr ><td width=142 height=120 >';
  67. strFrame+='    <table border=1 cellspacing=2 cellpadding=2 BORDERCOLORLIGHT=#63A3E9 BORDERCOLORDARK=#FFFFFF bgcolor=#fff8ec width=140 height=120 >';
  68. var n=0; for (j=0;j<5;j++){ strFrame+= ' <tr align=center >'for (i=0;i<7;i++){
  69. strFrame+='<td width=20 height=20 id=meizzDay'+n+' style="font-size:12px" οnclick=parent.meizzDayClick(this.innerText,0)></td>';n++;}
  70. strFrame+='</tr>';}
  71. strFrame+='      <tr align=center >';
  72. for (i=35;i<37;i++)strFrame+='<td width=20 height=20 id=meizzDay'+i+' style="font-size:12px"  οnclick="parent.meizzDayClick(this.innerText,0)"></td>';
  73. strFrame+='        <td colspan=5 align=right style="color:#1478eb"><span οnclick="parent.setNull()" style="font-size:12px;cursor: hand"';
  74. strFrame+='         οnmοuseοver="style.color=/'#ff0000/'" οnmοuseοut="style.color=/'#1478eb/'" title="将日期置空">置空</span>  <span οnclick="parent.meizzToday()" style="font-size:12px;cursor: hand"';
  75. strFrame+='         οnmοuseοver="style.color=/'#ff0000/'" οnmοuseοut="style.color=/'#1478eb/'" title="当前日期时间">当前</span>  <span style="cursor:hand" id=evaAllOK οnmοuseοver="style.color=/'#ff0000/'" οnmοuseοut="style.color=/'#1478eb/'"  οnclick="parent.closeLayer()" title="关闭日历">关闭 </span></td></tr>';
  76. strFrame+='    </table></td></tr><tr ><td >';
  77. strFrame+='        <table border=0 cellspacing=1 cellpadding=0 width=100%  bgcolor=#FFFFFF height=22 >';
  78. strFrame+='          <tr bgcolor="#63A3E9"><td id=bUseTimeLayer width=30  style="cursor:hand" title="点击这里启用/禁用时间"';
  79. strFrame+='             οnmοuseοver="style.backgroundColor=/'#aaccf3/'" align=center οnmοuseοut="style.backgroundColor=/'#63A3E9/'"';
  80. strFrame+='              οnclick="parent.UseTime(this)">';
  81. strFrame+=' <span></span></td>';
  82. strFrame+='             <td style="cursor:hand" οnclick="parent.tmpSelectHourInnerHTML(this.innerText.length==3?this.innerText.substring(0,1):this.innerText.substring(0,2))"';
  83. strFrame+=' οnmοuseοver="style.backgroundColor=/'#aaccf3/'" οnmοuseοut="style.backgroundColor=/'#63A3E9/'"';
  84. strFrame+=' title="点击这里选择时间" align=center width=42>' ;
  85. strFrame+='                 <span id=meizzHourHead></span></td>';
  86. strFrame+='             <td style="cursor:hand" οnclick="parent.tmpSelectMinuteInnerHTML(this.innerText.length==3?this.innerText.substring(0,1):this.innerText.substring(0,2))"';
  87. strFrame+=' οnmοuseοver="style.backgroundColor=/'#aaccf3/'" οnmοuseοut="style.backgroundColor=/'#63A3E9/'"';
  88. strFrame+=' title="点击这里选择时间" align=center width=42>' ;
  89. strFrame+='                 <span id=meizzMinuteHead></span></td>';
  90. strFrame+='             <td style="cursor:hand" οnclick="parent.tmpSelectSecondInnerHTML(this.innerText.length==3?this.innerText.substring(0,1):this.innerText.substring(0,2))"';
  91. strFrame+=' οnmοuseοver="style.backgroundColor=/'#aaccf3/'" οnmοuseοut="style.backgroundColor=/'#63A3E9/'"';
  92. strFrame+=' title="点击这里选择时间" align=center width=42>' ;
  93. strFrame+='                 <span id=meizzSecondHead></span></td>';
  94. strFrame+='          </tr></table></td></tr></table></div>';
  95. window.frames.endDateLayer.document.writeln(strFrame);
  96. window.frames.endDateLayer.document.close();        //解决ie进度条不结束的问题
  97. //==================================================== WEB 页面显示部分 ======================================================
  98. var outObject;
  99. var outButton;      //点击的按钮
  100. var outDate="";     //存放对象的日期
  101. var bUseTime=false;     //是否使用时间
  102. var odatelayer=window.frames.endDateLayer.document.all;     //存放日历对象
  103. var odatelayer=window.endDateLayer.document.all;
  104. //odatelayer.bUseTimeLayer.innerText="NO";
  105. bImgSwitch();
  106. odatelayer.bUseTimeLayer.innerHTML=bImg;
  107. function setday(tt,obj) //主调函数
  108. {
  109.     
  110.     if (arguments.length > 2){alert("对不起!传入本控件的参数太多!");return;}
  111.     if (arguments.length == 0){alert("对不起!您没有传回本控件任何参数!");return;}
  112.     var dads = document.all.endDateLayer.style;
  113.     var th = tt;
  114.     var ttop = tt.offsetTop;    //TT控件的定位点高
  115.     var thei = tt.clientHeight; //TT控件本身的高
  116.     var tleft = tt.offsetLeft;  //TT控件的定位点宽
  117.     var ttyp = tt.type; //TT控件的类型
  118.     while (tt = tt.offsetParent){ttop+=tt.offsetTop; tleft+=tt.offsetLeft;}
  119.     dads.top = (ttyp=="image") ? ttop+thei : ttop+thei+6;
  120.     dads.left = tleft;
  121.     outObject = (arguments.length == 1) ? th : obj;
  122.     outButton = (arguments.length == 1) ? null : th;    //设定外部点击的按钮
  123.     //根据当前输入框的日期显示日历的年月
  124.     var reg = /^(/d+)-(/d{1,2})-(/d{1,2})/;     //不含时间
  125.     var r = outObject.value.match(reg);
  126.     if(r!=null){
  127.         r[2]=r[2]-1;
  128.         var d=new Date(r[1],r[2],r[3]);
  129.         if(d.getFullYear()==r[1] && d.getMonth()==r[2] && d.getDate()==r[3])
  130.         {
  131.             outDate=d;
  132.             parent.meizzTheYear = r[1];
  133.             parent.meizzTheMonth = r[2];
  134.             parent.meizzTheDate = r[3];
  135.         }
  136.         else
  137.         {
  138.             outDate="";
  139.         }
  140.         meizzSetDay(r[1],r[2]+1);
  141.     }
  142.     else
  143.     {
  144.         outDate="";
  145.         meizzSetDay(new Date().getFullYear(), new Date().getMonth() + 1);
  146.     }
  147.     dads.display = '';
  148.     //判断初始化时是否使用时间,非严格验证
  149.     //if (outObject.value.length>10)
  150.     //{
  151.         bUseTime=true;
  152.         bImgSwitch();
  153.         odatelayer.bUseTimeLayer.innerHTML=bImg;
  154.         meizzWriteHead(meizzTheYear,meizzTheMonth);
  155.     //}
  156.     //else
  157.     //{
  158.     //  bUseTime=false;
  159.     //  bImgSwitch();
  160.     //  odatelayer.bUseTimeLayer.innerHTML=bImg;
  161.     //  meizzWriteHead(meizzTheYear,meizzTheMonth);
  162.     //}
  163.     try
  164.     {
  165.         event.returnValue=false;
  166.     }
  167.     catch (e)
  168.     {
  169.         //此处排除错误,错误原因暂未找到。
  170.     }
  171. }
  172. var MonHead = new Array(12);    //定义阳历中每个月的最大天数
  173. MonHead[0] = 31; MonHead[1] = 28; MonHead[2] = 31; MonHead[3] = 30; MonHead[4]  = 31; MonHead[5]  = 30;
  174. MonHead[6] = 31; MonHead[7] = 31; MonHead[8] = 30; MonHead[9] = 31; MonHead[10] = 30; MonHead[11] = 31;
  175. var meizzTheYear=new Date().getFullYear(); //定义年的变量的初始值
  176. var meizzTheMonth=new Date().getMonth()+1; //定义月的变量的初始值
  177. var meizzTheDate=new Date().getDate();  //定义日的变量的初始值    
  178. var meizzTheHour=new Date().getHours(); //定义小时变量的初始值
  179. var meizzTheMinute=new Date().getMinutes();//定义分钟变量的初始值
  180. var meizzTheSecond=new Date().getSeconds();//定义秒变量的初始值
  181. var meizzWDay=new Array(37);    //定义写日期的数组
  182. function document.onclick() //任意点击时关闭该控件    //ie6的情况可以由下面的切换焦点处理代替
  183.     with(window.event)
  184.     {
  185.         if (srcElement != outObject && srcElement != outButton)
  186.         closeLayer();
  187.     }
  188. }
  189. function document.onkeyup()     //按Esc键关闭,切换焦点关闭
  190. {
  191.     if (window.event.keyCode==27){
  192.         if(outObject)outObject.blur();
  193.         closeLayer();
  194.     }
  195.     else if(document.activeElement)
  196.     {
  197.         if(document.activeElement != outObject && document.activeElement != outButton)
  198.         {
  199.             closeLayer();
  200.         }
  201.     }
  202. }
  203. function meizzWriteHead(yy,mm,ss)   //往 head 中写入当前的年与月
  204. {
  205.     odatelayer.meizzYearHead.innerText  = yy + " 年";
  206.     odatelayer.meizzMonthHead.innerText = format(mm) + " 月";
  207.     //插入当前小时、分
  208.     odatelayer.meizzHourHead.innerText=bUseTime?(meizzTheHour+" 时"):""
  209.     odatelayer.meizzMinuteHead.innerText=bUseTime?(meizzTheMinute+" 分"):"";
  210.     odatelayer.meizzSecondHead.innerText=bUseTime?(meizzTheSecond+" 秒"):"";
  211. }
  212. function tmpSelectYearInnerHTML(strYear) //年份的下拉框
  213. {
  214.     if (strYear.match(//D/)!=null){alert("年份输入参数不是数字!");return;}
  215.     var m = (strYear) ? strYear : new Date().getFullYear();
  216.     if (m < 1000 || m > 9999) {alert("年份值不在 1000 到 9999 之间!");return;}
  217.     var n = m - 50;
  218.     if (n < 1000) n = 1000;
  219.     if (n + 101 > 9999) n = 9974;
  220.     var s = " <select name=tmpSelectYear style='font-size: 12px' "
  221.     s += "οnblur='document.all.tmpSelectYearLayer.style.display=/"none/"' "
  222.     s += "οnchange='document.all.tmpSelectYearLayer.style.display=/"none/";"
  223.     s += "parent.meizzTheYear = this.value; parent.meizzSetDay(parent.meizzTheYear,parent.meizzTheMonth)'>/r/n";
  224.     var selectInnerHTML = s;
  225.     for (var i = n; i < n + 101; i++)
  226.     {
  227.         if (i == m) { selectInnerHTML += "<option value='" + i + "' selected>" + i + "年" + "</option>/r/n"; }
  228.         else { selectInnerHTML += "<option value='" + i + "'>" + i + "年" + "</option>/r/n"; }
  229.     }
  230.     selectInnerHTML += "</select>";
  231.     odatelayer.tmpSelectYearLayer.style.display="";
  232.     odatelayer.tmpSelectYearLayer.innerHTML = selectInnerHTML;
  233.     odatelayer.tmpSelectYear.focus();
  234. }
  235. function tmpSelectMonthInnerHTML(strMonth) //月份的下拉框
  236. {
  237.     if (strMonth.match(//D/)!=null){alert("月份输入参数不是数字!");return;}
  238.     var m = (strMonth) ? strMonth : new Date().getMonth() + 1;
  239.     var s = "   <select name=tmpSelectMonth style='font-size: 12px' "
  240.     s += "οnblur='document.all.tmpSelectMonthLayer.style.display=/"none/"' "
  241.     s += "οnchange='document.all.tmpSelectMonthLayer.style.display=/"none/";"
  242.     s += "parent.meizzTheMonth = this.value; parent.meizzSetDay(parent.meizzTheYear,parent.meizzTheMonth)'>/r/n";
  243.     var selectInnerHTML = s;
  244.     for (var i = 1; i < 13; i++)
  245.     {
  246.         if (i == m) { selectInnerHTML += "<option value='"+i+"' selected>"+i+"月"+"</option>/r/n"; }
  247.         else { selectInnerHTML += "<option value='"+i+"'>"+i+"月"+"</option>/r/n"; }
  248.     }
  249.     selectInnerHTML += "</select>";
  250.     odatelayer.tmpSelectMonthLayer.style.display="";
  251.     odatelayer.tmpSelectMonthLayer.innerHTML = selectInnerHTML;
  252.     odatelayer.tmpSelectMonth.focus();
  253. }
  254. /***** 增加 小时、分钟 ***/
  255. function tmpSelectHourInnerHTML(strHour) //小时的下拉框
  256. {
  257.     if (!bUseTime){return;}
  258.     if (strHour.match(//D/)!=null){alert("小时输入参数不是数字!");return;}
  259.     var m = (strHour) ? strHour : new Date().getHours();
  260.     var s = "<select name=tmpSelectHour style='font-size: 12px' "
  261.     s += "οnblur='document.all.tmpSelectHourLayer.style.display=/"none/"' "
  262.     s += "οnchange='document.all.tmpSelectHourLayer.style.display=/"none/";"
  263.     s += "parent.meizzTheHour = this.value; parent.evaSetTime(parent.meizzTheHour,parent.meizzTheMinute);'>/r/n";
  264.     var selectInnerHTML = s;
  265.     for (var i = 0; i < 24; i++)
  266.     {
  267.         if (i == m) { selectInnerHTML += "<option value='"+i+"' selected>"+i+"</option>/r/n"; }
  268.         else { selectInnerHTML += "<option value='"+i+"'>"+i+"</option>/r/n"; }
  269.     }
  270.     selectInnerHTML += "</select>";
  271.     odatelayer.tmpSelectHourLayer.style.display="";
  272.     odatelayer.tmpSelectHourLayer.innerHTML = selectInnerHTML;
  273.     odatelayer.tmpSelectHour.focus();
  274. }
  275. function tmpSelectMinuteInnerHTML(strMinute) //分钟的下拉框
  276. {
  277.     if (!bUseTime){return;}
  278.     if (strMinute.match(//D/)!=null){alert("分钟输入参数不是数字!");return;}
  279.     var m = (strMinute) ? strMinute : new Date().getMinutes();
  280.     var s = "<select name=tmpSelectMinute style='font-size: 12px' "
  281.     s += "οnblur='document.all.tmpSelectMinuteLayer.style.display=/"none/"' "
  282.     s += "οnchange='document.all.tmpSelectMinuteLayer.style.display=/"none/";"
  283.     s += "parent.meizzTheMinute = this.value; parent.evaSetTime(parent.meizzTheHour,parent.meizzTheMinute);'>/r/n";
  284.     var selectInnerHTML = s;
  285.     for (var i = 0; i < 60; i++)
  286.     {
  287.         if (i == m) { selectInnerHTML += "<option value='"+i+"' selected>"+i+"</option>/r/n"; }
  288.         else { selectInnerHTML += "<option value='"+i+"'>"+i+"</option>/r/n"; }
  289.     }
  290.     selectInnerHTML += "</select>";
  291.     odatelayer.tmpSelectMinuteLayer.style.display="";
  292.     odatelayer.tmpSelectMinuteLayer.innerHTML = selectInnerHTML;
  293.     odatelayer.tmpSelectMinute.focus();
  294. }
  295. function tmpSelectSecondInnerHTML(strSecond) //秒的下拉框
  296. {
  297.     if (!bUseTime){return;}
  298.     if (strSecond.match(//D/)!=null){alert("分钟输入参数不是数字!");return;}
  299.     var m = (strSecond) ? strSecond : new Date().getMinutes();
  300.     var s = "<select name=tmpSelectSecond style='font-size: 12px' "
  301.     s += "οnblur='document.all.tmpSelectSecondLayer.style.display=/"none/"' "
  302.     s += "οnchange='document.all.tmpSelectSecondLayer.style.display=/"none/";"
  303.     s += "parent.meizzTheSecond = this.value; parent.evaSetTime(parent.meizzTheHour,parent.meizzTheMinute,parent.meizzTheSecond);'>/r/n";
  304.     var selectInnerHTML = s;
  305.     for (var i = 0; i < 60; i++)
  306.     {
  307.         if (i == m) { selectInnerHTML += "<option value='"+i+"' selected>"+i+"</option>/r/n"; }
  308.         else { selectInnerHTML += "<option value='"+i+"'>"+i+"</option>/r/n"; }
  309.     }
  310.     selectInnerHTML += "</select>";
  311.     odatelayer.tmpSelectSecondLayer.style.display="";
  312.     odatelayer.tmpSelectSecondLayer.innerHTML = selectInnerHTML;
  313.     odatelayer.tmpSelectSecond.focus();
  314. }
  315. function closeLayer()   //这个层的关闭
  316. {
  317.     var o = document.getElementById("endDateLayer");
  318.     if (o != null)
  319.     {
  320.         o.style.display="none";
  321.     }
  322. }
  323. function showLayer()    //这个层的关闭
  324. {
  325.     document.all.endDateLayer.style.display="";
  326. }
  327. function IsPinYear(year)    //判断是否闰平年
  328. {
  329.     if (0==year%4&&((year%100!=0)||(year%400==0))) return true;else return false;
  330. }
  331. function GetMonthCount(year,month)  //闰年二月为29天
  332. {
  333.     var c=MonHead[month-1];if((month==2)&&IsPinYear(year)) c++;return c;
  334. }
  335. function GetDOW(day,month,year) //求某天的星期几
  336. {
  337.     var dt=new Date(year,month-1,day).getDay()/7; return dt;
  338. }
  339. function meizzPrevY()   //往前翻 Year
  340. {
  341.     if(meizzTheYear > 999 && meizzTheYear <10000){meizzTheYear--;}
  342.     else{alert("年份超出范围(1000-9999)!");}
  343.     meizzSetDay(meizzTheYear,meizzTheMonth);
  344. }
  345. function meizzNextY()   //往后翻 Year
  346. {
  347.     if(meizzTheYear > 999 && meizzTheYear <10000){meizzTheYear++;}
  348.     else{alert("年份超出范围(1000-9999)!");}
  349.     meizzSetDay(meizzTheYear,meizzTheMonth);
  350. }
  351. function setNull()
  352. {
  353.     outObject.value = '';
  354.     closeLayer();
  355. }
  356. function meizzToday()   //Today Button
  357. {
  358.     parent.meizzTheYear     = new Date().getFullYear();
  359.     parent.meizzTheMonth    = new Date().getMonth()+1;
  360.     parent.meizzTheDate     = new Date().getDate();
  361.     parent.meizzTheHour     = new Date().getHours();
  362.     parent.meizzTheMinute   = new Date().getMinutes();
  363.     parent.meizzTheSecond   = new Date().getSeconds();
  364.     var meizzTheSecond      = new Date().getSeconds();
  365.     if (meizzTheMonth<10 && meizzTheMonth.length<2) //格式化成两位数字
  366.     {
  367.         parent.meizzTheMonth="0"+parent.meizzTheMonth;
  368.     }
  369.     if (parent.meizzTheDate<10 && parent.meizzTheDate.length<2) //格式化成两位数字
  370.     {
  371.         parent.meizzTheDate="0"+parent.meizzTheDate;
  372.     }
  373.     //meizzSetDay(meizzTheYear,meizzTheMonth);
  374.     if(outObject)
  375.     {
  376.         if (bUseTime)
  377.         {
  378.             outObject.value= parent.meizzTheYear + "-" + format( parent.meizzTheMonth) + "-" + 
  379.                             format(parent.meizzTheDate) + " " + format(parent.meizzTheHour) + ":" + 
  380.                             format(parent.meizzTheMinute) + ":" + format(parent.meizzTheSecond); 
  381.                             //注:在这里你可以输出改成你想要的格式
  382.         }
  383.         else
  384.         {
  385.             outObject.value= parent.meizzTheYear + "-" + format( parent.meizzTheMonth) + "-" + 
  386.                             format(parent.meizzTheDate); //注:在这里你可以输出改成你想要的格式
  387.         }
  388.     }
  389.     closeLayer();
  390. }
  391. function meizzPrevM()   //往前翻月份
  392. {
  393.     if(meizzTheMonth>1){meizzTheMonth--}else{meizzTheYear--;meizzTheMonth=12;}
  394.     meizzSetDay(meizzTheYear,meizzTheMonth);
  395. }
  396. function meizzNextM()   //往后翻月份
  397. {
  398.     if(meizzTheMonth==12){meizzTheYear++;meizzTheMonth=1}else{meizzTheMonth++}
  399.     meizzSetDay(meizzTheYear,meizzTheMonth);
  400. }
  401. // TODO: 整理代码
  402. function meizzSetDay(yy,mm) //主要的写程序**********
  403. {
  404.     meizzWriteHead(yy,mm);
  405.     //设置当前年月的公共变量为传入值
  406.     meizzTheYear=yy;
  407.     meizzTheMonth=mm;
  408.     for (var i = 0; i < 37; i++){meizzWDay[i]=""};  //将显示框的内容全部清空
  409.     var day1 = 1,day2=1,firstday = new Date(yy,mm-1,1).getDay();    //某月第一天的星期几
  410.     for (i=0;i<firstday;i++)meizzWDay[i]=GetMonthCount(mm==1?yy-1:yy,mm==1?12:mm-1)-firstday+i+1    //上个月的最后几天
  411.     for (i = firstday; day1 < GetMonthCount(yy,mm)+1; i++) { meizzWDay[i]=day1;day1++; }
  412.     for (i=firstday+GetMonthCount(yy,mm);i<37;i++) { meizzWDay[i]=day2;day2++; }
  413.     for (i = 0; i < 37; i++)
  414.     {
  415.         var da = eval("odatelayer.meizzDay"+i)  //书写新的一个月的日期星期排列
  416.     if (meizzWDay[i]!="")
  417.     {
  418.         //初始化边框
  419.         da.borderColorLight="#63A3E9";
  420.         da.borderColorDark="#63A3E9";
  421.         da.style.color="#1478eb";
  422.         if(i<firstday)      //上个月的部分
  423.         {
  424.             da.innerHTML="<b><font color=#BCBABC>" + meizzWDay[i] + "</font></b>";
  425.             da.title=(mm==1?12:mm-1) +"月" + meizzWDay[i] + "日";
  426.             da.οnclick=Function("meizzDayClick(this.innerText,-1)");
  427.             if(!outDate)
  428.                 da.style.backgroundColor = ((mm==1?yy-1:yy) == new Date().getFullYear() && 
  429.                     (mm==1?12:mm-1) == new Date().getMonth()+1 && meizzWDay[i] == new Date().getDate()) ?
  430.                      "#5CEFA0":"#f5f5f5";
  431.             else
  432.             {
  433.                 da.style.backgroundColor =((mm==1?yy-1:yy)==outDate.getFullYear() && (mm==1?12:mm-1)== outDate.getMonth() + 1 && 
  434.                 meizzWDay[i]==outDate.getDate())? "#84C1FF" :
  435.                 (((mm==1?yy-1:yy) == new Date().getFullYear() && (mm==1?12:mm-1) == new Date().getMonth()+1 && 
  436.                 meizzWDay[i] == new Date().getDate()) ? "#5CEFA0":"#f5f5f5");
  437.                 //将选中的日期显示为凹下去
  438.                 if((mm==1?yy-1:yy)==outDate.getFullYear() && (mm==1?12:mm-1)== outDate.getMonth() + 1 && 
  439.                 meizzWDay[i]==outDate.getDate())
  440.                 {
  441.                     da.borderColorLight="#FFFFFF";
  442.                     da.borderColorDark="#63A3E9";
  443.                 }
  444.             }
  445.         }
  446.         else if (i>=firstday+GetMonthCount(yy,mm))      //下个月的部分
  447.         {
  448.             da.innerHTML="<b><font color=#BCBABC>" + meizzWDay[i] + "</font></b>";
  449.             da.title=(mm==12?1:mm+1) +"月" + meizzWDay[i] + "日";
  450.             da.οnclick=Function("meizzDayClick(this.innerText,1)");
  451.             if(!outDate)
  452.                 da.style.backgroundColor = ((mm==12?yy+1:yy) == new Date().getFullYear() && 
  453.                     (mm==12?1:mm+1) == new Date().getMonth()+1 && meizzWDay[i] == new Date().getDate()) ?
  454.                      "#5CEFA0":"#f5f5f5";
  455.             else
  456.             {
  457.                 da.style.backgroundColor =((mm==12?yy+1:yy)==outDate.getFullYear() && (mm==12?1:mm+1)== outDate.getMonth() + 1 && 
  458.                 meizzWDay[i]==outDate.getDate())? "#84C1FF" :
  459.                 (((mm==12?yy+1:yy) == new Date().getFullYear() && (mm==12?1:mm+1) == new Date().getMonth()+1 && 
  460.                 meizzWDay[i] == new Date().getDate()) ? "#5CEFA0":"#f5f5f5");
  461.                 //将选中的日期显示为凹下去
  462.                 if((mm==12?yy+1:yy)==outDate.getFullYear() && (mm==12?1:mm+1)== outDate.getMonth() + 1 && 
  463.                 meizzWDay[i]==outDate.getDate())
  464.                 {
  465.                     da.borderColorLight="#FFFFFF";
  466.                     da.borderColorDark="#63A3E9";
  467.                 }
  468.             }
  469.         }
  470.         else        //本月的部分
  471.         {
  472.             da.innerHTML="<b>" + meizzWDay[i] + "</b>";
  473.             da.title=mm +"月" + meizzWDay[i] + "日";
  474.             da.οnclick=Function("meizzDayClick(this.innerText,0)");     //给td赋予onclick事件的处理
  475.             //如果是当前选择的日期,则显示亮蓝色的背景;如果是当前日期,则显示暗黄色背景
  476.             if(!outDate)
  477.                 da.style.backgroundColor = (yy == new Date().getFullYear() && mm == new Date().getMonth()+1 && meizzWDay[i] == new Date().getDate())?
  478.                     "#5CEFA0":"#f5f5f5";
  479.             else
  480.             {
  481.                 da.style.backgroundColor =(yy==outDate.getFullYear() && mm== outDate.getMonth() + 1 && meizzWDay[i]==outDate.getDate())?
  482.                     "#84C1FF":((yy == new Date().getFullYear() && mm == new Date().getMonth()+1 && meizzWDay[i] == new Date().getDate())?
  483.                     "#5CEFA0":"#f5f5f5");
  484.                 //将选中的日期显示为凹下去
  485.                 if(yy==outDate.getFullYear() && mm== outDate.getMonth() + 1 && meizzWDay[i]==outDate.getDate())
  486.                 {
  487.                     da.borderColorLight="#FFFFFF";
  488.                     da.borderColorDark="#63A3E9";
  489.                 }
  490.             }
  491.         }
  492.                 da.style.cursor="hand"
  493.             }
  494.         else { da.innerHTML="";da.style.backgroundColor="";da.style.cursor="default"; }
  495.     }
  496. }
  497. function meizzDayClick(n,ex)    //点击显示框选取日期,主输入函数*************
  498. {
  499.     parent.meizzTheDate=n;
  500.     var yy=meizzTheYear;
  501.     var mm = parseInt(meizzTheMonth)+ex;    //ex表示偏移量,用于选择上个月份和下个月份的日期
  502.     var hh=meizzTheHour;
  503.     var mi=meizzTheMinute;
  504.     var se=meizzTheSecond;
  505.     //判断月份,并进行对应的处理
  506.     if(mm<1){
  507.         yy--;
  508.         mm=12+mm;
  509.     }
  510.     else if(mm>12){
  511.         yy++;
  512.         mm=mm-12;
  513.     }
  514.     if (mm < 10)    {mm = "0" + mm;}
  515.     if (hh<10)      {hh="0" + hh;}  //时
  516.     if (mi<10)      {mi="0" + mi;}  //分
  517.     if (se<10)      {se="0" + se;}  //秒
  518.     if (outObject)
  519.     {
  520.         if (!n) {   //outObject.value=""; 
  521.             return;}
  522.         if ( n < 10){n = "0" + n;}
  523.         WriteDateTo(yy,mm,n,hh,mi,se);
  524.         closeLayer(); 
  525.         if (bUseTime)
  526.         {
  527.             try
  528.             {
  529.                 outButton.click();
  530.             }
  531.             catch (e)
  532.             {
  533.                 setday(outObject);
  534.             }
  535.         }
  536.     }
  537.     else {closeLayer(); alert("您所要输出的控件对象并不存在!");}
  538. }
  539. function format(n)  //格式化数字为两位字符表示
  540. {
  541.     var m=new String();
  542.     var tmp=new String(n);
  543.     if (n<10 && tmp.length<2)
  544.     {
  545.         m="0"+n;
  546.     }
  547.     else
  548.     {
  549.         m=n;
  550.     }
  551.     return m;
  552. }
  553. function evaSetTime()       //设置用户选择的小时、分钟
  554. {
  555.     odatelayer.meizzHourHead.innerText=meizzTheHour+" 时";
  556.     odatelayer.meizzMinuteHead.innerText=meizzTheMinute+" 分";
  557.     odatelayer.meizzSecondHead.innerText=meizzTheSecond+" 秒";
  558.     WriteDateTo(meizzTheYear,meizzTheMonth,meizzTheDate,meizzTheHour,meizzTheMinute,meizzTheSecond)
  559. }
  560. function evaSetTimeNothing()    //设置时间控件为空
  561. {
  562.     odatelayer.meizzHourHead.innerText="";
  563.     odatelayer.meizzMinuteHead.innerText="";
  564.     odatelayer.meizzSecondHead.innerText="";
  565.     WriteDateTo(meizzTheYear,meizzTheMonth,meizzTheDate,meizzTheHour,meizzTheMinute,meizzTheSecond)
  566. }
  567. function evaSetTimeNow()    //设置时间控件为当前时间
  568. {
  569.     odatelayer.meizzHourHead.innerText=new Date().getHours()+" 时";
  570.     odatelayer.meizzMinuteHead.innerText=new Date().getMinutes()+" 分";
  571.     odatelayer.meizzSecondHead.innerText=new Date().getSeconds()+" 秒";
  572.     meizzTheHour = new Date().getHours();
  573.     meizzTheMinute = new Date().getMinutes();
  574.     meizzTheSecond = new Date().getSeconds();
  575.     WriteDateTo(meizzTheYear,meizzTheMonth,meizzTheDate,meizzTheHour,meizzTheMinute,meizzTheSecond)
  576. }
  577. function UseTime(ctl)
  578. {
  579.     bUseTime=!bUseTime;
  580.     if (bUseTime)
  581.     {
  582.         bImgSwitch();
  583.         ctl.innerHTML=bImg;
  584.         evaSetTime();       //显示时间,用户原来选择的时间
  585.         //evaSetTimeNow();  //显示当前时间
  586.     }
  587.     else
  588.     {
  589.         bImgSwitch();
  590.         ctl.innerHTML=bImg;
  591.         evaSetTimeNothing();
  592.     }
  593. }
  594. function WriteDateTo(yy,mm,n,hh,mi,se)
  595. {
  596.     if (bUseTime)
  597.     {
  598.         outObject.value= yy + "-" + format(mm) + "-" + format(n) + " " + format(hh) + ":" + format(mi) + ":" + format(se); //注:在这里你可以输出改成你想要的格式
  599.     }
  600.     else
  601.     {
  602.         outObject.value= yy + "-" + format(mm) + "-" + format(n); //注:在这里你可以输出改成你想要的格式
  603.     }
  604. }
  605. function bImgSwitch()
  606. {
  607.     if (bUseTime)
  608.     {
  609.         bImg="开启";
  610.     }
  611.     else
  612.     {
  613.         bImg="关闭";
  614.     }
  615. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值