• js 计算快速统计中用到的日期


    前言

          最近在做统计报表模块,其中查询条件用到了快速查询,主要为了方便客户统计查询常用的几个日期纬度,比如本周、上周、本月、上月、昨日。 使用js计算,主要用到了js Date、 getDate()、getDay(), 代码包括格式化日期函数。

    快速查询日期计算

    function NewDate(str)
         {
             str=str.split('-');
             var date=new Date();
             date.setUTCFullYear(str[0], str[1]-1, str[2]);
             date.setUTCHours(0, 0, 0, 0);
             return date;
    
         }
    
    //格式化日期格式    stime=stime.format("yyyyMMdd");	 
    Date.prototype.format = function (format) {
    	var o = {
    		"M+": this.getMonth() + 1, //month
    		"d+": this.getDate(), //day
    		"h+": this.getHours(), //hour
    		"m+": this.getMinutes(), //minute
    		"s+": this.getSeconds(), //second
    		"q+": Math.floor((this.getMonth() + 3) / 3), //quarter
    		"S": this.getMilliseconds() //millisecond
    	}
    	if (/(y+)/.test(format)) format = format.replace(RegExp.$1,
    	(this.getFullYear() + "").substr(4 - RegExp.$1.length));
    	for (var k in o) if (new RegExp("(" + k + ")").test(format))
    		format = format.replace(RegExp.$1,
    		RegExp.$1.length == 1 ? o[k] :
    		("00" + o[k]).substr(("" + o[k]).length));
    	return format;
    }
    
    var curDateTime = new Date();
    var nowYear = curDateTime.getFullYear();
    var nowMonth = curDateTime.getMonth();
    var nowDay = curDateTime.getDate();
    var nowDayOfWeek = curDateTime.getDay();
    
    console.log('year:'+nowYear+',month:'+nowMonth+',day:'+nowDay+',dayofweek:'+nowDayOfWeek);
    
    var start=new Date(),end=new Date();
    //1昨天
    //curDateTime.setDate(curDateTime.getDate()-1);
    //start=curDateTime.format("yyyyMMdd");
    //end=curDateTime.format("yyyyMMdd");
    //console.log("昨天:"+start+"  "+end);
    
    //2前天
    //curDateTime.setDate(curDateTime.getDate()-2);
    //start=curDateTime.format("yyyyMMdd");
    //end=curDateTime.format("yyyyMMdd");
    //console.log("前天:"+start+"  "+end);
    
    //本周
    //start=new Date(nowYear,nowMonth,(nowDay-nowDayOfWeek+1));
    //start=start.format("yyyyMMdd");
    //end==new Date(nowYear,nowMonth,curDateTime.getDate());
    //end=end.format("yyyyMMdd");
    //console.log("本周:"+start+"  "+end);
    
    //上周
    //start=new Date(nowYear,nowMonth,(nowDay-nowDayOfWeek-6));
    //start=start.format("yyyyMMdd");
    //curDateTime.setDate(nowDay-nowDayOfWeek);
    //end=curDateTime.format("yyyyMMdd");
    //console.log("上周:"+start+"  "+end);
    
    //本月
    //start=curDateTime.format("yyyyMM01");
    //本月的截至日期只统计到当前
    //end=curDateTime.format("yyyyMMdd");
    //console.log("本月:"+start+"  "+end);
    
    //上月
    start =new Date(nowYear,nowMonth-1,1);
    start=start.format("yyyyMMdd");
    end=new Date(nowYear,nowMonth,1);
    end.setDate(end.getDate()-1);
    end=end.format("yyyyMMdd");
    console.log("上月:"+start+"  "+end);
    

      

  • 相关阅读:
    sql server2008配置管理工具服务显示远程过程调用失败
    SQL基础增删改查
    常见浏览器的兼容问题
    【ASP.NET Web API教程】2.3.3 创建Admin控制器
    你不小心已“同意” 许多互联网“霸王条款”
    常见浏览器的兼容问题
    微信公众号开发及时获取当前用户Openid及注意事项
    CSS3新特性(阴影、动画、渐变、变形、伪元素等)
    element Cascader 多选 点击文字选中
    小兔子有一颗玻璃心,完整版【转】
  • 原文地址:https://www.cnblogs.com/sword-successful/p/6874591.html
Copyright © 2020-2023  润新知