二,日历控件

  1. /*------------------------------------------------------------------------
  2.   日期控件
  3.   function Cal_dropdown(edit,min,max)
  4.     弹出日历,可不给参数min和max,参数edit必须有   
  5.   function Cal_datevalid(edit,min,max)
  6.     检查edit中值是否为大于等于min,小于等于max的有效日期格式字符串。
  7.     是则返回 true,否则返回false
  8.     可不给参数min和max(字符串格式)
  9.     参数edit必须有,如果edit无,则必须是:edit为edit和img的父亲(如span、div)的第一个元素
  10.     
  11. */
  12. var Cal_popup = window.createPopup();
  13. var Cal_edit;
  14. var Cal_editdate = new Date();
  15. var Cal_maxdate;
  16. var Cal_mindate;
  17. function Cal_clearTime(thedate)
  18. {
  19.   thedate.setHours(0);
  20.   thedate.setMinutes(0);
  21.   thedate.setSeconds(0);
  22.   thedate.setMilliseconds(0);
  23. }
  24. var Cal_today = new Date();
  25. Cal_clearTime(Cal_today);
  26. //检查日期//
  27. //editctrl:日期控件输入框
  28. // false: 日期错误
  29. function ChkCZDate(editctrl)
  30. {
  31. if(editctrl.value=='')
  32. { alert('请输入日期!')
  33.   editctrl.select();
  34.   editctrl.focus();
  35.   return false;}
  36. if(!Cal_datevalid(editctrl,'1910-1-1','3000-1-1'))
  37. {
  38. alert('日期格式不正确,日期有效范围为1910年到3000年');
  39. editctrl.focus();
  40. return false;
  41. }
  42. else
  43.  return true;
  44. }
  45. function Cal_decDay(thedate,days)
  46. {
  47.   if (!days) days = 1;
  48.   thedate.setTime(thedate - days*24*60*60*1000);
  49. }
  50. function Cal_incMonth(year,month)
  51. {
  52. if (month == 11) {
  53.     month = 0;
  54.     year++;
  55.   } else month++;
  56.   Cal_writeHTML(year,month);
  57. }
  58. function Cal_decMonth(year,month)
  59. {
  60.  if (month == 0) {
  61.     month = 11;
  62.     year--;
  63.   } else month--;
  64.   Cal_writeHTML(year,month);
  65. }
  66. function Cal_decYear(year,month)
  67. {
  68.   Cal_writeHTML(year-1,month);
  69. }
  70. function Cal_incYear(year,month)
  71. {
  72.   Cal_writeHTML(year+1,month);
  73. }
  74. function Cal_writeHTML(theyear,themonth)
  75. {
  76.   var html=
  77.     '<div id="Cal_div1" style="width:231px;FONT-SIZE:9pt;background-color:#fffef5;border:black 1px solid">'+
  78.     '<TABLE style="border-bottom:black 1px solid;FONT-SIZE: 9pt;background-color:#08D74C;color:white;'+
  79.     'padding-top:2px;font-weight:bold;text-align:center" '+
  80.     'cellSpacing="0" cellPadding="0" width="100%" border="0">'+
  81.     '<TR><TD style="cursor:hand" align="left" width=24 οnmοuseοver="this.style.background=' +
  82.     "'#12AFF8';" + '"' + ' οnmοuseοut="this.style.background=' + "'#08D74C';" + '"' +
  83.     ' οnclick="parent.Cal_decYear(' + theyear + ',' + themonth + ');" '+
  84.     '><年</TD>'+
  85.     '<TD style="cursor:hand" align="left" width=24 οnmοuseοver="this.style.background=' +
  86.     "'#12AFF8';" + '"' + ' οnmοuseοut="this.style.background=' + "'#08D74C';" + '"' +
  87.     ' οnclick="parent.Cal_decMonth(' + theyear + ',' + themonth + ');" '+
  88.     '><月</TD>'+
  89.     '<TD align="center">';
  90.     
  91.   html += theyear + '年' + (themonth + 1) + '月</TD>'+
  92.     '<TD style="cursor:hand" align="right" width=24 οnmοuseοver="this.style.background=' +
  93.     "'#12AFF8';" + '"' + ' οnmοuseοut="this.style.background=' + "'#08D74C';" + '"' +
  94.     ' οnclick="parent.Cal_incMonth(' + theyear + ',' + themonth + ');" '+
  95.     '>月></TD>'+
  96.     '<TD style="cursor:hand" align="right" width=24 οnmοuseοver="this.style.background=' +
  97.     "'#12AFF8';" + '"' + ' οnmοuseοut="this.style.background=' + "'#08D74C';" + '"' +
  98.     ' οnclick="parent.Cal_incYear(' + theyear + ',' + themonth + ');" '+
  99.     '>年></TD>';
  100.     
  101.   html +=
  102.     '</TR></TABLE>'+
  103.     '<TABLE style="FONT-SIZE: 9pt;font-weight:bold;text-align:center;border-bottom:black 1px solid" '+
  104.     'cellSpacing="2" cellPadding="0" width="100%" border="0">'+
  105.     '<TR><TD>日</TD><TD>一</TD><TD>二</TD><TD>三</TD><TD>四</TD><TD>五</TD><TD>六</TD>'+
  106.     '</TR></table>'+
  107.     '<TABLE style="FONT-SIZE: 9pt;text-align:center;cursor:hand" cellSpacing="2" '+
  108.     'cellPadding="0" width="100%" border="0">';
  109.   var day1 = new Date(theyear,themonth,1);
  110.   if (day1.getDay()!=0)
  111.   Cal_decDay(day1,day1.getDay()); // 日历开始日
  112. for (var i=1;i<=6;i++) {
  113.     html += '<TR>';
  114. for (var j=1;j<=7;j++) {
  115.       html += '<TD';
  116.       if (day1.getTime()==Cal_today.getTime())
  117.         html += ' style="color:blue"';
  118.       else
  119.       if (day1.getTime()==Cal_editdate.getTime())
  120.         html += ' style="color:red"';
  121.       else
  122.       if (day1.getMonth() != themonth)
  123.         html += ' style="color:#aaaaaa"';
  124.       html += ' οnmοuseοver="this.style.background=' +
  125.               "'#12AFF8';" + '"'+
  126.               ' οnmοuseοut="this.style.background=' +
  127.               "'#fffef5';" + '"';
  128.       html += ' οnclick="parent.Cal_clickday('+day1.getTime() + ');"';
  129.       html +='>' + day1.getDate() + '</TD>';
  130.       Cal_decDay(day1,-1);
  131.     }
  132.     html += '</TR>';
  133.     if (day1.getMonth() != themonth) break;
  134.   }
  135.     
  136.   html +=
  137.     '</TABLE>'+
  138.     '<div style="border-top:black 1px solid;text-align:center;padding:2px">今天是 '+
  139.     '<span style="color:blue;cursor:hand;text-decoration:underline" οnclick="javascript:parent.Cal_clickday('+
  140.     Cal_today.getTime() + ');">'+
  141.     Cal_today.getFullYear() + '-' + (Cal_today.getMonth()+1) + '-' + Cal_today.getDate() +
  142.     '</span></div>'+
  143.     '</div>';
  144.   Cal_popup.document.body.innerHTML = html;
  145.   if (Cal_popup.isOpen) // 重新调整显示高度
  146.     Cal_popup.show(0, Cal_edit.offsetHeight, 231, Cal_popup.document.all("Cal_div1").offsetHeight,Cal_edit);
  147. }
  148. // 字符串转换为日期
  149. function Cal_strtodate(str)
  150. {
  151.   var date = Date.parse(str);
  152.  if (isNaN(date)) {
  153.     date = Date.parse(str.replace(/-/g,"/")); // 识别日期格式:YYYY-MM-DD
  154.     if (isNaN(date)) date = 0;
  155.   }
  156.   return(date);
  157. }
  158. //返回日期间相差的天数
  159. function Cal_DateDiff(Date1, Date2)
  160. {
  161.     return (Date2-Date1)/(24*60*60*1000);
  162. }
  163. //返回日期间相差的月数(最大误差小于一个月)
  164. function Cal_MonthDiff(DateA, DateB)
  165. {
  166.     Date1=new Date();
  167.     Date2=new Date();
  168.     Date1.setTime(DateA);
  169.     Date2.setTime(DateB);
  170.     months = (Date2.getFullYear() - Date1.getFullYear()) * 12;
  171.     addmonths = Date2.getMonth() - Date1.getMonth();
  172.     months = months + addmonths;
  173.     if(Date2.getDate() < Date1.getDate())
  174.         months--;
  175.     return months;
  176. }
  177. // 弹出日历,可不给参数min和max,参数edit必须有
  178. function Cal_dropdown(edit,min,max) {
  179. if (!edit) {
  180.     edit = window.event.srcElement.parentElement.children(0);
  181.     if ((!edit.type) || (edit.type.toLowerCase() != "text")) return;
  182.   }
  183.   Cal_edit = edit;
  184.   var date = Cal_strtodate(edit.value);
  185.   if (date == 0) date = Cal_today.getTime();
  186.   if (max) Cal_maxdate = Cal_strtodate(max);
  187.   else Cal_maxdate=0;
  188.   if (min) Cal_mindate = Cal_strtodate(min);
  189.   else Cal_mindate = 0;
  190.   Cal_editdate.setTime(date);
  191.   Cal_writeHTML(Cal_editdate.getFullYear(),Cal_editdate.getMonth());
  192.   Cal_popup.show(0, edit.offsetHeight, 231, 149,edit);
  193.   Cal_popup.show(0, edit.offsetHeight, 231, Cal_popup.document.all("Cal_div1").offsetHeight,edit);
  194. }
  195. // 点击日期
  196. function Cal_clickday(day,edit)
  197. {
  198.   if (Cal_maxdate != 0) day = Math.min(day,Cal_maxdate);
  199.   day = Math.max(day,Cal_mindate);
  200.   Cal_editdate.setTime(day);
  201.   var im;
  202.   var id;
  203.   if ((Cal_editdate.getMonth()+1)<10)
  204.   {
  205.    im = Cal_editdate.getMonth()+1;
  206.    im = '0' + im;
  207.   }
  208.   else
  209.    im = Cal_editdate.getMonth()+1;
  210.   if ((Cal_editdate.getDate())<10)
  211.    id = "0"+Cal_editdate.getDate();
  212.   else
  213.    id = Cal_editdate.getDate();
  214.   Cal_edit.value = Cal_editdate.getFullYear() + "-" + im + "-"
  215.                    + id;
  216.   Cal_popup.hide();
  217.   Cal_edit.fireEvent("onkeydown");
  218.   Cal_edit.focus();
  219. }
  220. function Cal_datevalid(edit,min,max)
  221. {
  222.   // 检查edit中值是否为大于等于min,小于等于max的有效日期格式字符串。
  223.   var date = Cal_strtodate(edit.value);
  224.   if (date == 0) return false;
  225.   if (max) {
  226.     var max = Cal_strtodate(max);
  227.     if ((max!=0)&&(date>max)) return false;
  228.   }
  229.    if (min) {
  230.     var min = Cal_strtodate(min);
  231.     if ((min!=0)&&(date<min)) return false;
  232.   }
  233.   date = new Date(date);
  234.   edit.value = date.getFullYear() + "-" + (date.getMonth()+1) +
  235.                "-" + date.getDate();
  236.   return true;
  237. }

<asp:textbox id="dtOpenTime" runat="server" Width="100px" visible="True" ></asp:textbox>
               <img src="img/calendar.gif" alt="打开日历" id="imgdtOpenTimeDate" style="CURSOR: hand;" οnclick="javascript:Cal_dropdown(dtOpenTime)" />

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值