JS日期选择器

  1 <html>
  2 <head>
  3 <title>JS日期选择器</title>
  4 <script type="text/javascript">
  5 function HS_DateAdd(interval,number,date){
  6     number = parseInt(number);
  7     if (typeof(date)=="string"){var date = new Date(date.split("-")[0],date.split("-")[1],date.split("-")[2])}
  8     if (typeof(date)=="object"){var date = date}
  9     switch(interval){
 10     case "y":return new Date(date.getFullYear()+number,date.getMonth(),date.getDate()); break;
 11     case "m":return new Date(date.getFullYear(),date.getMonth()+number,checkDate(date.getFullYear(),date.getMonth()+number,date.getDate())); break;
 12     case "d":return new Date(date.getFullYear(),date.getMonth(),date.getDate()+number); break;
 13     case "w":return new Date(date.getFullYear(),date.getMonth(),7*number+date.getDate()); break;
 14     }
 15 }
 16 function checkDate(year,month,date){
 17     var enddate = ["31","28","31","30","31","30","31","31","30","31","30","31"];
 18     var returnDate = "";
 19     if (year%4==0){enddate[1]="29"}
 20     if (date>enddate[month]){returnDate = enddate[month]}else{returnDate = date}
 21     return returnDate;
 22 }
 23 
 24 function WeekDay(date){
 25     var theDate;
 26     if (typeof(date)=="string"){theDate = new Date(date.split("-")[0],date.split("-")[1],date.split("-")[2]);}
 27     if (typeof(date)=="object"){theDate = date}
 28     return theDate.getDay();
 29 }
 30 function HS_calender(){
 31     var lis = "";
 32     var style = "";
 33     style +="<style type='text/css'>";
 34     style +=".calender { 170px; height:auto; font-size:12px; margin-right:14px; background:url(calenderbg.gif) no-repeat right center #fff; border:1px solid #397EAE; padding:1px}";
 35     style +=".calender ul {list-style-type:none; margin:0; padding:0;}";
 36     style +=".calender .day { background-color:#EDF5FF; height:20px;}";
 37     style +=".calender .day li,.calender .date li{ float:left; 14%; height:20px; line-height:20px; text-align:center}";
 38     style +=".calender li a { text-decoration:none; font-family:Tahoma; font-size:11px; color:#333}";
 39     style +=".calender li a:hover { color:#f30; text-decoration:underline}";
 40     style +=".calender li a.hasArticle {font-weight:bold; color:#f60 !important}";
 41     style +=".lastMonthDate, .nextMonthDate {color:#bbb;font-size:11px}";
 42     style +=".selectThisYear a, .selectThisMonth a{text-decoration:none; margin:0 2px; color:#000; font-weight:bold}";
 43     style +=".calender .LastMonth, .calender .NextMonth{ text-decoration:none; color:#000; font-size:18px; font-weight:bold; line-height:16px;}";
 44     style +=".calender .LastMonth { float:left;}";
 45     style +=".calender .NextMonth { float:right;}";
 46     style +=".calenderBody {clear:both}";
 47     style +=".calenderTitle {text-align:center;height:20px; line-height:20px; clear:both}";
 48     style +=".today { background-color:#ffffaa;border:1px solid #f60; padding:2px}";
 49     style +=".today a { color:#f30; }";
 50     style +=".calenderBottom {clear:both; border-top:1px solid #ddd; padding: 3px 0; text-align:left}";
 51     style +=".calenderBottom a {text-decoration:none; margin:2px !important; font-weight:bold; color:#000}";
 52     style +=".calenderBottom a.closeCalender{float:right}";
 53     style +=".closeCalenderBox {float:right; border:1px solid #000; background:#fff; font-size:9px; 11px; height:11px; line-height:11px; text-align:center;overflow:hidden; font-weight:normal !important}";
 54     style +="</style>";
 55 
 56     var now;
 57     if (typeof(arguments[0])=="string"){
 58         selectDate = arguments[0].split("-");
 59         var year = selectDate[0];
 60         var month = parseInt(selectDate[1])-1+"";
 61         var date = selectDate[2];
 62         now = new Date(year,month,date);
 63     }else if (typeof(arguments[0])=="object"){
 64         now = arguments[0];
 65     }
 66     var lastMonthEndDate = HS_DateAdd("d","-1",now.getFullYear()+"-"+now.getMonth()+"-01").getDate();
 67     var lastMonthDate = WeekDay(now.getFullYear()+"-"+now.getMonth()+"-01");
 68     var thisMonthLastDate = HS_DateAdd("d","-1",now.getFullYear()+"-"+(parseInt(now.getMonth())+1).toString()+"-01");
 69     var thisMonthEndDate = thisMonthLastDate.getDate();
 70     var thisMonthEndDay = thisMonthLastDate.getDay();
 71     var todayObj = new Date();
 72     today = todayObj.getFullYear()+"-"+todayObj.getMonth()+"-"+todayObj.getDate();
 73     
 74     for (i=0; i<lastMonthDate; i++){  // Last Month's Date
 75         lis = "<li class='lastMonthDate'>"+lastMonthEndDate+"</li>" + lis;
 76         lastMonthEndDate--;
 77     }
 78     for (i=1; i<=thisMonthEndDate; i++){ // Current Month's Date
 79 
 80         if(today == now.getFullYear()+"-"+now.getMonth()+"-"+i){
 81             var todayString = now.getFullYear()+"-"+(parseInt(now.getMonth())+1).toString()+"-"+i;
 82             lis += "<li><a href=javascript:void(0) class='today' onclick='_selectThisDay(this)' title='"+now.getFullYear()+"-"+(parseInt(now.getMonth())+1)+"-"+i+"'>"+i+"</a></li>";
 83         }else{
 84             lis += "<li><a href=javascript:void(0) onclick='_selectThisDay(this)' title='"+now.getFullYear()+"-"+(parseInt(now.getMonth())+1)+"-"+i+"'>"+i+"</a></li>";
 85         }
 86         
 87     }
 88     var j=1;
 89     for (i=thisMonthEndDay; i<6; i++){  // Next Month's Date
 90         lis += "<li class='nextMonthDate'>"+j+"</li>";
 91         j++;
 92     }
 93     lis += style;
 94 
 95     var CalenderTitle = "<a href='javascript:void(0)' class='NextMonth' onclick=HS_calender(HS_DateAdd('m',1,'"+now.getFullYear()+"-"+now.getMonth()+"-"+now.getDate()+"'),this) title='Next Month'>&raquo;</a>";
 96     CalenderTitle += "<a href='javascript:void(0)' class='LastMonth' onclick=HS_calender(HS_DateAdd('m',-1,'"+now.getFullYear()+"-"+now.getMonth()+"-"+now.getDate()+"'),this) title='Previous Month'>&laquo;</a>";
 97     CalenderTitle += "<span class='selectThisYear'><a href='javascript:void(0)' onclick='CalenderselectYear(this)' title='Click here to select other year' >"+now.getFullYear()+"</a></span>年<span class='selectThisMonth'><a href='javascript:void(0)' onclick='CalenderselectMonth(this)' title='Click here to select other month'>"+(parseInt(now.getMonth())+1).toString()+"</a></span>月"; 
 98 
 99     if (arguments.length>1){
100         arguments[1].parentNode.parentNode.getElementsByTagName("ul")[1].innerHTML = lis;
101         arguments[1].parentNode.innerHTML = CalenderTitle;
102 
103     }else{
104         var CalenderBox = style+"<div class='calender'><div class='calenderTitle'>"+CalenderTitle+"</div><div class='calenderBody'><ul class='day'><li>日</li><li>一</li><li>二</li><li>三</li><li>四</li><li>五</li><li>六</li></ul><ul class='date' id='thisMonthDate'>"+lis+"</ul></div><div class='calenderBottom'><a href='javascript:void(0)' class='closeCalender' onclick='closeCalender(this)'>×</a><span><span><a href=javascript:void(0) onclick='_selectThisDay(this)' title='"+todayString+"'>Today</a></span></span></div></div>";
105         return CalenderBox;
106     }
107 }
108 function _selectThisDay(d){
109     var boxObj = d.parentNode.parentNode.parentNode.parentNode.parentNode;
110         boxObj.targetObj.value = d.title;
111         boxObj.parentNode.removeChild(boxObj);
112 }
113 function closeCalender(d){
114     var boxObj = d.parentNode.parentNode.parentNode;
115         boxObj.parentNode.removeChild(boxObj);
116 }
117 
118 function CalenderselectYear(obj){
119         var opt = "";
120         var thisYear = obj.innerHTML;
121         for (i=1970; i<=2020; i++){
122             if (i==thisYear){
123                 opt += "<option value="+i+" selected>"+i+"</option>";
124             }else{
125                 opt += "<option value="+i+">"+i+"</option>";
126             }
127         }
128         opt = "<select onblur='selectThisYear(this)' onchange='selectThisYear(this)' style='font-size:11px'>"+opt+"</select>";
129         obj.parentNode.innerHTML = opt;
130 }
131 
132 function selectThisYear(obj){
133     HS_calender(obj.value+"-"+obj.parentNode.parentNode.getElementsByTagName("span")[1].getElementsByTagName("a")[0].innerHTML+"-1",obj.parentNode);
134 }
135 
136 function CalenderselectMonth(obj){
137         var opt = "";
138         var thisMonth = obj.innerHTML;
139         for (i=1; i<=12; i++){
140             if (i==thisMonth){
141                 opt += "<option value="+i+" selected>"+i+"</option>";
142             }else{
143                 opt += "<option value="+i+">"+i+"</option>";
144             }
145         }
146         opt = "<select onblur='selectThisMonth(this)' onchange='selectThisMonth(this)' style='font-size:11px'>"+opt+"</select>";
147         obj.parentNode.innerHTML = opt;
148 }
149 function selectThisMonth(obj){
150     HS_calender(obj.parentNode.parentNode.getElementsByTagName("span")[0].getElementsByTagName("a")[0].innerHTML+"-"+obj.value+"-1",obj.parentNode);
151 }
152 function HS_setDate(inputObj){
153     var calenderObj = document.createElement("span");
154     calenderObj.innerHTML = HS_calender(new Date());
155     calenderObj.style.position = "absolute";
156     calenderObj.targetObj = inputObj;
157     inputObj.parentNode.insertBefore(calenderObj,inputObj.nextSibling);
158 }
159   </script>
160 <style>
161   body {font-size:12px}
162   td {text-align:center}
163   h1 {font-size:26px;}
164   h4 {font-size:16px;}
165   em {color:#999; margin:0 10px; font-size:11px; display:block}
166   </style>
167 </head>
168 <body>
169 <h4 style="border-bottom:1px solid #ccc">ver:1.0</h4>
170 <table border="1" width="400" height="150">
171     <tr>
172         <td>这是示例文字</td>
173         <td>示例输入框</td>
174         <td>文本文本文本</td>
175     </tr>
176     <tr>
177         <td>示例输入框</td>
178         <td><input type="text" style="70px" onfocus="HS_setDate(this)">文本</td>
179         <td>这里是你的文字</td>
180     </tr>
181     <tr>
182         <td>一段文字</td>
183         <td>示例输入框</td>
184         <td>文本<input type="text" style="70px" onfocus="HS_setDate(this)">文本</td>
185     </tr>
186 </table>
187 <ul>
188     <li>它不需要其他框架类支持</li>
189     <li>支持多种浏览器</li>
190     <li>点击年份、月份可下拉选择</li>
191 </ul>
192 </body>
193 </html>
原文地址:https://www.cnblogs.com/changweihua/p/2543830.html