js获取本日、本周、本月的时间代码
本日时间
functionshowToDay() { varNowdate=newDate(); M=Number(Nowdate.getMonth())+1 alert(Nowdate.getMonth()+"月,"+Nowdate.getDate()+"号,星期"+Nowdate.getDay()); returnNowdate.getYear()+"-"+M+"-"+Nowdate.getDate(); }
昨天
functionshowTomorrow() { vartom=newDate(); tom.setDate(tom.getDate()+1); M=Number(tom.getMonth())+1 returntom.getYear()+"-"+M+"-"+tom.getDate(); }
本周第一天
functionshowWeekFirstDay() { varNowdate=newDate(); varWeekFirstDay=newDate(Nowdate-(Nowdate.getDay()-1)*86400000); M=Number(WeekFirstDay.getMonth())+1 returnWeekFirstDay.getYear()+"-"+M+"-"+WeekFirstDay.getDate(); }
本周最后天
functionshowWeekLastDay() { varNowdate=newDate(); varWeekFirstDay=newDate(Nowdate-(Nowdate.getDay()-1)*86400000); varWeekLastDay=newDate((WeekFirstDay/1000+6*86400)*1000); M=Number(WeekLastDay.getMonth())+1 returnWeekLastDay.getYear()+"-"+M+"-"+WeekLastDay.getDate(); }
本月第一天
functionshowMonthFirstDay() { varNowdate=newDate(); varMonthFirstDay=newDate(Nowdate.getYear(),Nowdate.getMonth(),1); M=Number(MonthFirstDay.getMonth())+1 returnMonthFirstDay.getYear()+"-"+M+"-"+MonthFirstDay.getDate(); }
本月最后一天
functionshowMonthLastDay() { varNowdate=newDate(); varMonthNextFirstDay=newDate(Nowdate.getYear(),Nowdate.getMonth()+1,1); varMonthLastDay=newDate(MonthNextFirstDay-86400000); M=Number(MonthLastDay.getMonth())+1 returnMonthLastDay.getYear()+"-"+M+"-"+MonthLastDay.getDate(); } functiongetTime(s) { vartime1=document.getElementById("num1"); vartime2=document.getElementById("num2"); if(s==1){ //本日时间 time1.value=showToDay(); //alert("今日:"+time1.value); time2.value=showTomorrow(); //alert("今日:"+time2.value); }elseif(s==2){ //本周时间 time1.value=showWeekFirstDay(); //alert("本月第一天:"+time1.value); time2.value=showWeekLastDay(); //alert("本月最后一天:"+time2.value); }else{ //本月时间 time1.value=showMonthFirstDay(); //alert("本月第一天"+time1.value); time2.value=showMonthLastDay(); //alert("本月最后一天"+time2.value); } }
其他网友补充的完整测试代码
Document