美文网首页
js处理日期,今天,昨天,本周,上周,上个月,本月的开始日期和结

js处理日期,今天,昨天,本周,上周,上个月,本月的开始日期和结

作者: 甘道夫老矣 | 来源:发表于2023-08-09 15:07 被阅读0次
     // tab点击
          const  handleTabItem = function (value) {
                $scope.activeMenu = value;
                let d = new Date();
                let startDate = null;
                let endDate = null;
                if (value === '全部') {
                    startDate = null;
                    endDate = null;
                } else if (value === '今天') {
                    startDate = new Date(new Date().toLocaleDateString()).getTime();
                    endDate = new Date(new Date().toLocaleDateString()).getTime() + 24 * 60 * 60 * 1000 - 1;
                } else if (value === '昨天') {
                    d.setDate(d.getDate() - 1);
                    startDate = new Date(d.toLocaleDateString()).getTime()
                    endDate = new Date(d.toLocaleDateString()).getTime() + 24 * 60 * 60 * 1000 - 1;
                } else if (value === '本周') {
                    let week = d.getDay();//返回date是一周中的某一天
                    let millisecond = 1000 * 60 * 60 * 24;  //一天的毫秒数
                    let minusDay = week != 0 ? week - 1 : 6; //减去的天数  
                    startDate = new Date(new Date(d.getTime() - (minusDay * millisecond)).setHours(0, 0, 0, 0)).getTime(); //本周 周一 0:00          
                    endDate = new Date(new Date(startDate + 6 * millisecond).setHours(0, 0, 0, 0)).getTime() + 86399999;//本周 周日 23:59       
                } else if (value === '上周') {
                    let week = d.getDay();//返回date是一周中的某一天
                    let millisecond = 1000 * 60 * 60 * 24;  //一天的毫秒数
                    let minusDay = week != 0 ? week - 1 : 6; //减去的天数  
                    let currentWeekDayOne = new Date(d.getTime() - (millisecond * minusDay)); //获得当前周的第一天    
                    endDate = new Date(new Date(currentWeekDayOne.getTime() - millisecond).setHours(0, 0, 0, 0)).getTime() + 86399999; //上周最后一天即本周开始的前一天 0:00   
                    startDate = new Date(new Date(endDate - (millisecond * 6)).setHours(0, 0, 0, 0)).getTime(); //上周的第一天 23:59    
    
                } else if (value === '本月') {
                    let currentMonth = d.getMonth();//获得当前月份0-11 
                    let currentYear = d.getFullYear();//获得当前年份4位年 
                    startDate = new Date(new Date(currentYear, currentMonth, 1).setHours(0, 0, 0, 0)).getTime();//求出本月第一天    
                    if (currentMonth == 11) {//当为12月的时候年份需要加1 月份需要更新为0 也就是下一年的第一个月     
                        currentYear++;
                        currentMonth = 0;//就为0    
                    } else {//否则只是月份增加,以便求的下一月的第一天      8080
                        currentMonth++;
                    }
                    let millisecond = 1000 * 60 * 60 * 24;//一天的毫秒数   
                    let nextMonthDayOne = new Date(currentYear, currentMonth, 1); //下月的第一天    
                    endDate = new Date(new Date(nextMonthDayOne.getTime() - millisecond).setHours(0, 0, 0, 0)).getTime() + 86399999;//求出上月的最后一天    
                } else if (value === '上月') {
                    var obj = getLastMonthStartDate();
                    startDate = new Date(obj.startDate).getTime();
                    endDate = new Date(obj.endDate).getTime();
                }
             
                // startDate = new Date(startDate)
                // endDate = new Date(endDate)
                // console.log(startDate, endDate);
            
    
            }
    
     //获得上月开始和结束时间
            function getLastMonthStartDate() {
                var nowdays = new Date();
                var year = nowdays.getFullYear();
                var month = nowdays.getMonth();
                if (month == 0) {
                    month = 12;
                    year = year - 1;
                }
                if (month < 10) {
                    month = '0' + month;
                }
                var myDate = new Date(year, month, 0);
                var startDate = year + '-' + month + '-01 00:00:00'; //上个月第一天
                var endDate = year + '-' + month + '-' + myDate.getDate() + ' 23:59:00';//上个月最后一天
                var objtime = { startDate: startDate, endDate: endDate }
                return objtime;
            };
    
    

    相关文章

      网友评论

          本文标题:js处理日期,今天,昨天,本周,上周,上个月,本月的开始日期和结

          本文链接:https://www.haomeiwen.com/subject/cjtapdtx.html