前言

最近在做统计报表模块,其中查询条件用到了快速查询,主要为了方便客户统计查询常用的几个日期纬度,比如本周、上周、本月、上月、昨日。 使用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);

  

js 计算快速统计中用到的日期的更多相关文章

  1. JS计算本周一和本周五的日期

    代码不长: var today=new Date();var weekday=today.getDay();    var monday=new Date(1000*60*60*24*(1-weekd ...

  2. js计算2个日期相差的天数,两个日期相差的天数,日期相隔天数

    js计算2个日期相差的天数,两个日期相差的天数,日期相隔天数 >>>>>>>>>>>>>>>>>& ...

  3. js计算两个日期的天数差值

    js计算两个日期的天数差值 通过两个日期计算这两个日期之间的天数差值 /** * 计算天数差的函数,通用 * @param sDate1 * @param sDate2 * @returns {Num ...

  4. JS计算两个日期之间的天数

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  5. JS计算字符串所占字节数

    最近项目有个需求要用js计算一串字符串写入到localStorage里所占的内存,众所周知的,js是使用Unicode编码的.而Unicode的实现有N种,其中用的最多的就是UTF-8和UTF-16. ...

  6. Vue (一) --- vue.js的快速入门使用

    =-----------------------------------把现在的工作做好,才能幻想将来的事情,专注于眼前的事情,对于尚未发生的事情而陷入无休止的忧虑之中,对事情毫无帮助,反而为自己凭添 ...

  7. three.js - 动画 图形统计帧频 dat.GUI

    运行一把: 代码解释: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...

  8. JS计算前一天或后一天,前一月后一月

    JS计算前一天或后一天,前一月后一月,上一天下一下,上一月下一月. 方法一: function ktkGetNextMonth(currentDate, scaleStep) { //scaleSte ...

  9. Excel 中如何快速统计一列中相同字符的个数(函数法)

    https://jingyan.baidu.com/article/6d704a132ea17328da51ca78.html 通过excel快速统计一列中相同字符的个数,如果很少,你可以一个一个数. ...

随机推荐

  1. [C#]GetFloat提示"指定的转换无效"

    数据库中没有double型,float就表示double值.sql server数据库字段类型与.net的数据类型的对应关系: real(数据库)<--> float(.NET)float ...

  2. 别人的Linux私房菜(5)首次CentOS7与帮助等

    ctrl alt F1-F6切换终端tty1-6,其中,F1的终端带有用户界面. 在终端登录后,输入startx启动个人图形界面.(启动有一些条件限制,如没有其他的X Window启用,已经安装,并具 ...

  3. 利用 Python 练习数据挖掘

    本文由 伯乐在线 - 顾星竹 翻译,Namco 校稿.未经许可,禁止转载!英文出处:Giuseppe Vettigli.欢迎加入翻译组. 覆盖使用Python进行数据挖掘查找和描述数据结构模式的实践工 ...

  4. verilog 有符号数运算

    1)之前的笔记写过<补码探讨>,可知在FPGA综合成电路的时候最底层都是以补码的形式在运算,正数的补码就是本身,负数的补码要取反+1. (2)那么Verilog中编程的时候对编程人员来说, ...

  5. Linux 线程编程2.0——线程同步-互斥锁

    当我们需要控制对共享资源的存取的时候,可以用一种简单的加锁的方法来控制.我们可以创建一个读/写程序,它们共用一个共享缓冲区,使用互斥锁来控制对缓冲区的存取. 函数 pthread_mutex_init ...

  6. 带权单源最短路发[稠密图](Dijkstra)

    对于稠密图,采用邻接矩阵较为合适 所以我们先构建一个邻接矩阵 typedef int Vertex; typedef int WeightType; //图 typedef struct MyGrap ...

  7. css3的动画效果

    全新的css3加入的动画效果: [ animation-name ]:检索或设置对象所应用的动画名称 [ animation-duration ]: 检索或设置对象动画的持续时间 [ animatio ...

  8. Redis简明教程

    redis是什么: Redis is an open source, BSD licensed, advanced key-value store. It is often referred to a ...

  9. 一个简单的C语言题背后的故事

    最近看到了一个C语言问题,是要计算出这个函数的输出: #include <stdio.h> int Test(int x,int y, int z){ printf("x,y,z ...

  10. 机器学习基石笔记:12 Nonlinear Transformation

    一.二次假设 实际上线性假设的模型复杂度是受到限制的, 需要高次假设打破这个限制. 假设数据不是线性可分的,但是可以被一个圆心在原点的圆分开, 需要我们重新设计基于该圆的PLA等算法吗? 不用, 只需 ...