date.asp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <script language="JavaScript" type="text/JavaScript">
  2. //http://hi.baidu.com/jiecol
  3. var months = new Array("一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月");
  4. var daysInMonth = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
  5. var days = new Array("日","一", "二", "三", "四", "五", "六");
  6. var today;
  7. document.writeln("<div id='Calendar' style='position:absolute; z-index:1; visibility: hidden; filter:\"progid:DXImageTransform.Microsoft.Shadow(direction=135,color=#999999,strength=3)\"'></div>");
  8. function getDays(month, year)
  9. {
  10. //下面的这段代码是判断当前是否是闰年的
  11. if (1 == month)
  12. return ((0 == year % 4) && (0 != (year % 100))) || (0 == year % 400) ? 29 : 28;
  13. else
  14. return daysInMonth[month];
  15. }
  16. function getToday()
  17. {
  18. //得到今天的年,月,日
  19. this.now = new Date();
  20. this.year = this.now.getFullYear();
  21. this.month = this.now.getMonth();
  22. this.day = this.now.getDate();
  23. }
  24. function getStringDay(str)
  25. {
  26. //得到输入框的年,月,日
  27. var str=str.split("-")
  28. this.now = new Date(parseFloat(str[0]),parseFloat(str[1])-1,parseFloat(str[2]));
  29. this.year = this.now.getFullYear();
  30. this.month = this.now.getMonth();
  31. this.day = this.now.getDate();
  32. }
  33. function newCalendar() {
  34. var parseYear = parseInt(document.all.Year.options[document.all.Year.selectedIndex].value);
  35. var newCal = new Date(parseYear, document.all.Month.selectedIndex, 1);
  36. var day = -1;
  37. var startDay = newCal.getDay();
  38. var daily = 0;
  39. if ((today.year == newCal.getFullYear()) &&(today.month == newCal.getMonth()))
  40. day = today.day;
  41. var tableCal = document.all.calendar;
  42. var intDaysInMonth =getDays(newCal.getMonth(), newCal.getFullYear());
  43. for (var intWeek = 1; intWeek < tableCal.rows.length;intWeek++)
  44. for (var intDay = 0;intDay < tableCal.rows[intWeek].cells.length;intDay++)
  45. {
  46. var cell = tableCal.rows[intWeek].cells[intDay];
  47. if ((intDay == startDay) && (0 == daily))
  48. daily = 1;
  49. if(day==daily) //今天,调用今天的Class
  50. {
  51. cell.style.background='#6699CC';
  52. cell.style.color='#FFFFFF';
  53. //cell.style.fontWeight='bold';
  54. }
  55. else if(intDay==6) //周六
  56. cell.style.color='green';
  57. else if (intDay==0) //周日
  58. cell.style.color='red';
  59. if ((daily > 0) && (daily <= intDaysInMonth))
  60. {
  61. cell.innerText = daily;
  62. daily++;
  63. }
  64. else
  65. cell.innerText = "";
  66. }
  67. }
  68. function GetDate(InputBox)
  69. {
  70. var sDate;
  71. //这段代码处理鼠标点击的情况
  72. if (event.srcElement.tagName == "TD")
  73. if (event.srcElement.innerText != "")
  74. {
  75. sDate = document.all.Year.value + "-" + document.all.Month.value + "-" + event.srcElement.innerText;
  76. eval("document.all."+InputBox).value=sDate;
  77. HiddenCalendar();
  78. }
  79. }
  80. function HiddenCalendar()
  81. {
  82. //关闭选择窗口
  83. document.all.Calendar.style.visibility='hidden';
  84. }
  85. function ShowCalendar(InputBox)
  86. {
  87. var x,y,intLoop,intWeeks,intDays;
  88. var DivContent;
  89. var year,month,day;
  90. var o=eval("document.all."+InputBox);
  91. var thisyear; //真正的今年年份
  92. thisyear=new getToday();
  93. thisyear=thisyear.year;
  94. today = o.value;
  95. if(isDate(today))
  96. today = new getStringDay(today);
  97. else
  98. today = new getToday();
  99. //显示的位置
  100. x=o.offsetLeft;
  101. y=o.offsetTop;
  102. while(o=o.offsetParent)
  103. {
  104. x+=o.offsetLeft;
  105. y+=o.offsetTop;
  106. }
  107. document.all.Calendar.style.left=x+2;
  108. document.all.Calendar.style.top=y+21;
  109. document.all.Calendar.style.visibility="visible";
  110. //下面开始输出日历表格(border-color:#9DBAF7)
  111. DivContent="<table border='0' cellspacing='0' style='border:1px solid #0066FF; background-color:#EDF2FC'>";
  112. DivContent+="<tr>";
  113. DivContent+="<td style='border-bottom:1px solid #0066FF; background-color:#C7D8FA'>";
  114. //年
  115. DivContent+="<select name='Year' id='Year' onChange='newCalendar()' style='font-family:Verdana; font-size:12px'>";
  116. for (intLoop = thisyear - 12; intLoop < (thisyear + 2); intLoop++)
  117. DivContent+="<option value= " + intLoop + " " + (today.year == intLoop ? "Selected" : "") + ">" + intLoop + "</option>";
  118. DivContent+="</select>";
  119. //月
  120. DivContent+="<select name='Month' id='Month' onChange='newCalendar()' style='font-family:Verdana; font-size:12px'>";
  121. for (intLoop = 0; intLoop < months.length; intLoop++)
  122. DivContent+="<option value= " + (intLoop + 1) + " " + (today.month == intLoop ? "Selected" : "") + ">" + months[intLoop] + "</option>";
  123. DivContent+="</select>";
  124. DivContent+="</td>";
  125. DivContent+="<td style='border-bottom:1px solid #0066FF; background-color:#C7D8FA; font-weight:bold; font-family:Wingdings 2,Wingdings,Webdings; font-size:16px; padding-top:2px; color:#4477FF; cursor:hand' align='center' title='关闭' onClick='javascript:HiddenCalendar()'>S</td>";
  126. DivContent+="</tr>";
  127. DivContent+="<tr><td align='center' colspan='2'>";
  128. DivContent+="<table id='calendar' border='0' width='100%'>";
  129. //星期
  130. DivContent+="<tr>";
  131. for (intLoop = 0; intLoop < days.length; intLoop++)
  132. DivContent+="<td align='center' style='font-size:12px'>" + days[intLoop] + "</td>";
  133. DivContent+="</tr>";
  134. //天
  135. for (intWeeks = 0; intWeeks < 6; intWeeks++)
  136. {
  137. DivContent+="<tr>";
  138. for (intDays = 0; intDays < days.length; intDays++)
  139. DivContent+="<td onClick='GetDate(\"" + InputBox + "\")' style='cursor:hand; border-right:1px solid #BBBBBB; border-bottom:1px solid #BBBBBB; color:#215DC6; font-family:Verdana; font-size:12px' align='center'></td>";
  140. DivContent+="</tr>";
  141. }
  142. DivContent+="</table></td></tr></table>";
  143. document.all.Calendar.innerHTML=DivContent;
  144. newCalendar();
  145. }
  146. function isDate(dateStr)
  147. {
  148. var datePat = /^(\d{4})(\-)(\d{1,2})(\-)(\d{1,2})$/;
  149. var matchArray = dateStr.match(datePat);
  150. if (matchArray == null) return false;
  151. var month = matchArray[3];
  152. var day = matchArray[5];
  153. var year = matchArray[1];
  154. if (month < 1 || month > 12) return false;
  155. if (day < 1 || day > 31) return false;
  156. if ((month==4 || month==6 || month==9 || month==11) && day==31) return false;
  157. if (month == 2)
  158. {
  159. var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
  160. if (day > 29 || (day==29 && !isleap)) return false;
  161. }
  162. return true;
  163. }
  164. </script>