js如何获取本日 本周 本月的时间代码
程序员文章站
2022-03-28 21:36:31
...
本篇文章给大家分享的内容是利用js如何获取本日 本周 本月的时间代码,有着一定的参考价值,有需要的朋友可以参考一下
function showToDay() { var Nowdate=new Date(); M=Number(Nowdate.getMonth())+1 alert(Nowdate.getMonth()+"月,"+Nowdate.getDate()+"号,星期"+Nowdate.getDay()); return Nowdate.getYear()+"-"+M+"-"+Nowdate.getDate(); }
function showTomorrow() { var tom=new Date(); tom.setDate(tom.getDate()+1); M=Number(tom.getMonth())+1 return tom.getYear()+"-"+M+"-"+tom.getDate(); }
本周第一天
function showWeekFirstDay() { var Nowdate=new Date(); var WeekFirstDay=new Date(Nowdate-(Nowdate.getDay()-1)*86400000); M=Number(WeekFirstDay.getMonth())+1 return WeekFirstDay.getYear()+"-"+M+"-"+WeekFirstDay.getDate(); }
本周最后天
function showWeekLastDay() { var Nowdate=new Date(); var WeekFirstDay=new Date(Nowdate-(Nowdate.getDay()-1)*86400000); var WeekLastDay=new Date((WeekFirstDay/1000+6*86400)*1000); M=Number(WeekLastDay.getMonth())+1 return WeekLastDay.getYear()+"-"+M+"-"+WeekLastDay.getDate(); }
本月第一天
function showMonthFirstDay() { var Nowdate=new Date(); var MonthFirstDay=new Date(Nowdate.getYear(),Nowdate.getMonth(),1); M=Number(MonthFirstDay.getMonth())+1 return MonthFirstDay.getYear()+"-"+M+"-"+MonthFirstDay.getDate(); }
本月最后一天
function showMonthLastDay() { var Nowdate=new Date(); var MonthNextFirstDay=new Date(Nowdate.getYear(),Nowdate.getMonth()+1,1); var MonthLastDay=new Date(MonthNextFirstDay-86400000); M=Number(MonthLastDay.getMonth())+1 return MonthLastDay.getYear()+"-"+M+"-"+MonthLastDay.getDate(); }
function getTime(s) { var time1=document.getElementById("num1"); var time2=document.getElementById("num2"); if(s==1){ //本日时间 time1.value=showToDay(); //alert("今日:"+time1.value); time2.value=showTomorrow(); //alert("今日:"+time2.value); }else if(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); } }
以上就是js如何获取本日 本周 本月的时间代码的详细内容,更多请关注其它相关文章!
上一篇: 微信开发需要注意哪些事项?
下一篇: 微信小程序网络API 上传、下载详细说明