使用my97datepicker 4.0控件,需要对日期的范围进行控制,本人自己写了一个js完成此功能,示例为当前日期到下一周周五之间的日期可选,其他日期不可选,关键代码如下:
1 <head runat="server"> 2 <title></title> 3 <script type="text/javascript"> 4 function getNextFriday() { 5 var myDate = new Date(); 6 var year = myDate.getFullYear(); //得到当前年份 7 var month = myDate.getMonth() + 1; //得到当前月份 8 var day = myDate.getDate(); //得到当前日 9 var weekDay = myDate.getDay(); //得到当前的星期X(0-6代表星期日-星期六) 10 var maxDayOfMonth = new Date(year, month, 0).getDate(); //得到当前月最大日 11 12 var nextFriday;//距离下周五的天数 13 14 switch (weekDay) { 15 case 0: 16 nextFriday = 5; 17 break; 18 case 1: 19 nextFriday = 11; 20 break; 21 case 2: 22 nextFriday = 10; 23 break; 24 case 3: 25 nextFriday = 9; 26 break; 27 case 4: 28 nextFriday = 8; 29 break; 30 case 5: 31 nextFriday = 7; 32 break; 33 case 6: 34 nextFriday = 6; 35 break; 36 default: 37 nextFriday = 1; 38 break; 39 } 40 41 var dayTotal = day + nextFriday; 42 43 /** 44 * 1、如果当前日期到下周五所在的日期在当月,即下周五的日期未跨年也为跨月 45 * 2、跨月,且并未跨年 46 * 3、跨年 47 **/ 48 if (dayTotal < maxDayOfMonth) { 49 if (month < 10) { 50 month = "0" + month; 51 } 52 if (dayTotal < 10) { 53 dayTotal = "0" + dayTotal; 54 } 55 return year + "-" + month + "-" + dayTotal; 56 } else if (month < 12) { 57 month += 1; 58 if (month < 10) { 59 month = "0" + month; 60 } 61 var nextMonthDay = nextFriday - (maxDayOfMonth - day); 62 if (nextMonthDay < 10) { 63 nextMonthDay = "0" + nextMonthDay; 64 } 65 return year + "-" + month + "-" + nextMonthDay; 66 } else { 67 year += 1; 68 month = "01"; 69 var nextMonthDay = nextFriday - (maxDayOfMonth - day); 70 if (nextMonthDay < 10) { 71 nextMonthDay = "0" + nextMonthDay; 72 } 73 return year + "-" + month + "-" + nextMonthDay; 74 } 75 } 76 </script> 77 </head> 78 <body> 79 <form id="form1" runat="server"> 80 <input id="DateText" type="text" runat="server" onfocus="var friday=getNextFriday();WdatePicker({minDate:'%y-%M-%d',maxDate:friday,el:$dp.$('DateText')})" /> 81 </form> 82 </body>