项目需求:
<input type="hidden" id="today">
<script> //把时间类型的数字转换为各种需要的字符串 function MM_dateFormat(rq, fenge) { //格式化日期显示 //fenge = 0 :2006-02-28 12:34:23 //fenge = 1 :2006-02-28 //fenge = 2 :2006年2月28日 //fenge = 3 :2月28日 //fenge = 4 :28/2 //fenge = 5 :二○○六年二月二十八日 //fenge = 6 :12:34:23 //fenge = 7 :20060228 if((fenge == null) || (fenge == "")) fenge = 0; if((rq == "") || (rq == null)) var tmpDate = new Date(); else var tmpDate = new Date(rq); var result = ""; switch(fenge) { case 1: result += tmpDate.getFullYear() + "-"; if((tmpDate.getMonth() + 1) < 10) result += "0" + (tmpDate.getMonth() + 1) + "-"; else result += (tmpDate.getMonth() + 1) + "-"; if(tmpDate.getDate() < 10) result += "0" + tmpDate.getDate(); else result += tmpDate.getDate(); break; case 2: result = tmpDate.getFullYear() + "年" + (tmpDate.getMonth() + 1) + "月" + tmpDate.getDate() + "日"; break; case 3: result = (tmpDate.getMonth() + 1) + "月" + tmpDate.getDate() + "日"; break; case 4: result = tmpDate.getDate() + "/" + (tmpDate.getMonth() + 1); break; case 5: var zhifu = "○一二三四五六七八九十"; var zhrq = ""; var nan = String(tmpDate.getFullYear()); for(iii = 0; iii < nan.length; iii++) { zhrq += zhifu.substr(nan.substr(iii, 1), 1); } zhrq += "年"; var nan = String(tmpDate.getMonth() + 1); if(nan.length == 2) { if(nan.substr(0, 1) == "1") zhrq += "十"; else zhrq += zhifu.substr(nan.substr(0, 1), 1) + "十"; if(nan.substr(1, 1) == "0") zhrq += "月"; else zhrq += zhifu.substr(nan.substr(1, 1), 1) + "月" } else { zhrq += zhifu.substr(nan.substr(0, 1), 1) + "月"; } var nan = String(tmpDate.getDate()); if(nan.length == 2) { if(nan.substr(0, 1) == "1") zhrq += "十"; else zhrq += zhifu.substr(nan.substr(0, 1), 1) + "十"; if(nan.substr(1, 1) == "0") zhrq += "日"; else zhrq += zhifu.substr(nan.substr(1, 1), 1) + "日" } else { zhrq += zhifu.substr(nan.substr(0, 1), 1) + "日"; } result = zhrq; break; case 6: if(tmpDate.getHours() < 10) result += "0" + tmpDate.getHours(); else result += "" + tmpDate.getHours(); if(tmpDate.getMinutes() < 10) result += ":0" + tmpDate.getMinutes(); else result += ":" + tmpDate.getMinutes(); if(tmpDate.getSeconds() < 10) result += ":0" + tmpDate.getSeconds(); else result += ":" + tmpDate.getSeconds(); break; case 7: result += tmpDate.getFullYear(); if((tmpDate.getMonth() + 1) < 10) result += "0" + (tmpDate.getMonth() + 1); else result += (tmpDate.getMonth() + 1); if(tmpDate.getDate() < 10) result += "0" + tmpDate.getDate(); else result += tmpDate.getDate(); break; default: result += tmpDate.getFullYear() + "-"; if((tmpDate.getMonth() + 1) < 10) result += "0" + (tmpDate.getMonth() + 1) + "-"; else result += (tmpDate.getMonth() + 1) + "-"; if(tmpDate.getDate() < 10) result += "0" + tmpDate.getDate(); else result += tmpDate.getDate(); if(tmpDate.getHours() < 10) result += " 0" + tmpDate.getHours(); else result += " " + tmpDate.getHours(); if(tmpDate.getMinutes() < 10) result += ":0" + tmpDate.getMinutes(); else result += ":" + tmpDate.getMinutes(); if(tmpDate.getSeconds() < 10) result += ":0" + tmpDate.getSeconds(); else result += ":" + tmpDate.getSeconds(); break; } return result; } //得到每周的第一天(周一) function getFirstDateOfWeek(theDate){ var firstDateOfWeek; theDate.setDate(theDate.getDate() + 1 - theDate.getDay()); firstDateOfWeek = theDate; return firstDateOfWeek; } //得到每周的最后一天(周日) function getLastDateOfWeek(theDate){ var lastDateOfWeek; theDate.setDate(theDate.getDate() +7 - theDate.getDay()); lastDateOfWeek = theDate; return lastDateOfWeek; } //得到上周的第一天(周一) function getPreviousFirstDateOfWeek(theDate){ var firstDateOfWeek; theDate.setDate(theDate.getDate() - 6 - theDate.getDay()); firstDateOfWeek = theDate; return firstDateOfWeek; } //得到上周的最后一天(周日) function getPreviousLastDateOfWeek(theDate){ var lastDateOfWeek; theDate.setDate(theDate.getDate() +7 - theDate.getDay()); lastDateOfWeek = theDate; return lastDateOfWeek; } //得到下周的第一天(周一) function getNextFirstDateOfWeek(theDate){ var firstDateOfWeek; theDate.setDate(theDate.getDate() + 8 - theDate.getDay()); firstDateOfWeek = theDate; return firstDateOfWeek; } //得到下周的最后一天(周日) function getNextLastDateOfWeek(theDate){ var lastDateOfWeek; theDate.setDate(theDate.getDate() +7 - theDate.getDay()); lastDateOfWeek = theDate; return lastDateOfWeek; }
//将时间格式的字符串转为时间格式 function strToDate(dateStr){ var year = dateStr.substr(0,4); var month = dateStr.substr(5,2); var day = dateStr.substr(8,2); var temDate = month+'/'+day+'/'+year; return new Date(temDate); } $(document).ready(function(){ $("input[name=user_type]").click(function(){ var flag=''; $("input[name=user_type]").filter(':checked').each(function(){ flag+=$(this).val(); }); if(flag=='') return false; var date=$("#today").val(); get(date) }); //上周 $("#pre").click(function(){ var tD=strToDate($("#today").val()); var first = MM_dateFormat(getPreviousFirstDateOfWeek(tD).toString(),1); var last= MM_dateFormat(getPreviousLastDateOfWeek(tD).toString(),1); $("#today").val(first); $("#curr").html(first+'-'+last); get(first); }).addClass("pointer"); //下周 $("#next").click(function(){ var tD=strToDate($("#today").val()); var first = MM_dateFormat(getNextFirstDateOfWeek(tD).toString(),1); var last= MM_dateFormat(getNextLastDateOfWeek(tD).toString(),1); $("#today").val(first); $("#curr").html(first+'-'+last); get(first); }).addClass("pointer"); var theDate="<?php echo date('Y-m-d');?>"; $("#today").val(theDate); var tD=strToDate(theDate); var nWeekFirstDay = getFirstDateOfWeek(tD).toString(); var nWeekLastDay= getLastDateOfWeek(tD).toString(); var ch = MM_dateFormat(nWeekFirstDay,1); var ch1 = MM_dateFormat(nWeekLastDay,1); //alert("本周一的时间是:" + ch); //alert("本周日的时间是:" + ch1); $("#curr").html(ch+'-'+ch1); get($("#today").val()); });
<table width="100%" border="0" style="background-color:#6a6a6a;color:#fff;vertical-align:middle;height:50px;"> <tr> <td class="linkcss" style="text-align:left;"><span style="padding-left:20px;background:url(images/resultset_previous.png) no-repeat 0px 0px;" id="pre">上一周</span></td> <td style="text-align:center;padding:5px 0px 5px 0px;font-size:30px;font-weight:bold;" id="curr"></td> <td class="linkcss" style="text-align:right;"><span style="padding-right:20px;background:url(images/resultset_next.png) no-repeat 40px 0px;" id="next">下一周</span></td> </tr> </table>