zoukankan      html  css  js  c++  java
  • 计算上月、下月、上周、下周..日期范围

    <script language=javascript>
    //今天
    function showToDay()
    {
    var Nowdate=new Date();
    M=Number(Nowdate.getMonth())+1
    return Nowdate.getYear()+"-"+M+"-"+Nowdate.getDate();
    }

    //本周第一天
    function showWeekFirstDay()
    {
    var Nowdate=new Date();
    var WeekFirstDay=new Date(Nowdate-(Nowdate.getDay()-1)*86400000);
    return WeekFirstDay;
    }
    //本周最后一天
    function showWeekLastDay()
    {
    var Nowdate=new Date();
    var WeekFirstDay=new Date(Nowdate-(Nowdate.getDay()-1)*86400000);
    var WeekLastDay=new Date((WeekFirstDay/1000+6*86400)*1000);
    return WeekLastDay;
    }

    //本月第一天
    function showMonthFirstDay()
    {
    var Nowdate=new Date();
    var MonthFirstDay=new Date(Nowdate.getYear(),Nowdate.getMonth(),1);
    return MonthFirstDay;
    }
    //本月最后一天
    function showMonthLastDay()
    {
    var Nowdate=new Date();
    var MonthNextFirstDay=new Date(Nowdate.getYear(),Nowdate.getMonth()+1,1);
    var MonthLastDay=new Date(MonthNextFirstDay-86400000);
    return MonthLastDay;
    }
    //上月第一天
    function showPreviousFirstDay()
    {
    var MonthFirstDay=showMonthFirstDay()
    return new Date(MonthFirstDay.getYear(),MonthFirstDay.getMonth()-1,1)
    }
    //上月最后一天
    function showPreviousLastDay()
    {
    var MonthFirstDay=showMonthFirstDay();
    return new Date(MonthFirstDay-86400000);
    }
    //上周第一天
    function showPreviousFirstWeekDay()
    {
    var WeekFirstDay=showWeekFirstDay()
    return new Date(WeekFirstDay-86400000*7)
    }
    //上周最后一天
    function showPreviousLastWeekDay()
    {
    var WeekFirstDay=showWeekFirstDay()
    return new Date(WeekFirstDay-86400000)
    }
    //上一天
    function showPreviousDay()
    {
    var MonthFirstDay=new Date();
    return new Date(MonthFirstDay-86400000);
    }
    //下一天
    function showNextDay()
    {
    var MonthFirstDay=new Date();
    return new Date((MonthFirstDay/1000+86400)*1000);
    }
    //下周第一天
    function showNextFirstWeekDay()
    {
    var MonthFirstDay=showWeekLastDay()
    return new Date((MonthFirstDay/1000+86400)*1000)
    }
    //下周最后一天
    function showNextLastWeekDay()
    {
    var MonthFirstDay=showWeekLastDay()
    return new Date((MonthFirstDay/1000+7*86400)*1000)
    }
    //下月第一天
    function showNextFirstDay()
    {
    var MonthFirstDay=showMonthFirstDay()
    return new Date(MonthFirstDay.getYear(),MonthFirstDay.getMonth()+1,1)
    }
    //下月最后一天
    function showNextLastDay()
    {
    var MonthFirstDay=showMonthFirstDay()
    return new Date(new Date(MonthFirstDay.getYear(),MonthFirstDay.getMonth()+2,1)-86400000)
    }
    function Date.prototype.toString(){
    return this.getFullYear()+"-"+(this.getMonth()+1)+"-"+this.getDate();
    }
    function setDate(num){
    if(num==1){
    document.all.dateDate.value=showMonthFirstDay();
    document.all.dateDate2.value=showMonthLastDay();
    }
    if(num==2){
    document.all.dateDate.value=showWeekFirstDay();
    document.all.dateDate2.value=showWeekLastDay();
    }
    if(num==3){
    document.all.dateDate.value=showToDay();
    document.all.dateDate2.value=showToDay();
    }
    if(num==4){
    document.all.dateDate.value=showPreviousFirstDay();
    document.all.dateDate2.value=showPreviousLastDay();
    }
    if(num==5){
    document.all.dateDate.value=showNextFirstDay();
    document.all.dateDate2.value=showNextLastDay();
    }
    if(num==6){
    document.all.dateDate.value=showPreviousFirstWeekDay();
    document.all.dateDate2.value=showPreviousLastWeekDay();
    }
    if(num==7){
    document.all.dateDate.value=showNextFirstWeekDay();
    document.all.dateDate2.value=showNextLastWeekDay();
    }
    if(num==8){
    document.all.dateDate.value=showPreviousDay();
    document.all.dateDate2.value=showPreviousDay();
    }
    if(num==9){
    document.all.dateDate.value=showNextDay();
    document.all.dateDate2.value=showNextDay();
    }
    }

    </script>
    <input name=dateDate type=text>-><input name=dateDate2 type=text>
    &nbsp;<input name=haha type=button value="上一月" onclick=setDate(4)>&nbsp;
    &nbsp;<input name=haha type=button value="本月" onclick=setDate(1)>&nbsp;
    <input name=haha type=button value="下一月" onclick=setDate(5)>&nbsp;
    <input name=haha type=button value="上一周" onclick=setDate(6)>&nbsp;
    <input name=haha type=button value="本周" onclick=setDate(2)>&nbsp;
    <input name=haha type=button value="下一周" onclick=setDate(7)>&nbsp;
    <input name=haha type=button value="上一天" onclick=setDate(8)>&nbsp;
    <input name=haha type=button value="今天" onclick=setDate(3)>&nbsp;
    <input name=haha type=button value="下一天" onclick=setDate(9)>
  • 相关阅读:
    开发新技术展望系列课程(视频课程讲师:徐晓卓)
    VSTS风暴系列课程(视频课程讲师:王京京/王兴明/王然)
    Mysql索引的数据结构及索引优化
    CAP原则,分布式场景下为何只能取其二
    为什么使用Redission解决高并发场景分布式锁问题
    Java面试题(6)Redis
    外企英语面试常见问题及核心话术
    Nacos&Eureka&Zookeeper
    j2ee中DAO设计模式
    第一个随笔
  • 原文地址:https://www.cnblogs.com/MaxIE/p/419144.html
Copyright © 2011-2022 走看看