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);
js 计算快速统计中用到的日期的更多相关文章
- JS计算本周一和本周五的日期
代码不长: var today=new Date();var weekday=today.getDay(); var monday=new Date(1000*60*60*24*(1-weekd ...
- js计算2个日期相差的天数,两个日期相差的天数,日期相隔天数
js计算2个日期相差的天数,两个日期相差的天数,日期相隔天数 >>>>>>>>>>>>>>>>>& ...
- js计算两个日期的天数差值
js计算两个日期的天数差值 通过两个日期计算这两个日期之间的天数差值 /** * 计算天数差的函数,通用 * @param sDate1 * @param sDate2 * @returns {Num ...
- JS计算两个日期之间的天数
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- JS计算字符串所占字节数
最近项目有个需求要用js计算一串字符串写入到localStorage里所占的内存,众所周知的,js是使用Unicode编码的.而Unicode的实现有N种,其中用的最多的就是UTF-8和UTF-16. ...
- Vue (一) --- vue.js的快速入门使用
=-----------------------------------把现在的工作做好,才能幻想将来的事情,专注于眼前的事情,对于尚未发生的事情而陷入无休止的忧虑之中,对事情毫无帮助,反而为自己凭添 ...
- three.js - 动画 图形统计帧频 dat.GUI
运行一把: 代码解释: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...
- JS计算前一天或后一天,前一月后一月
JS计算前一天或后一天,前一月后一月,上一天下一下,上一月下一月. 方法一: function ktkGetNextMonth(currentDate, scaleStep) { //scaleSte ...
- Excel 中如何快速统计一列中相同字符的个数(函数法)
https://jingyan.baidu.com/article/6d704a132ea17328da51ca78.html 通过excel快速统计一列中相同字符的个数,如果很少,你可以一个一个数. ...
随机推荐
- sock5客户端解密数据流
一.安装 略 二.配置 vi /etc/shadowsocks.json { "server":"x.x.x.x", , , "password&qu ...
- VMware虚拟机网络设置
背景介绍 在用 VMware workstation 安装好虚拟机后,需要给虚拟机配置网络,配置网络的方法有桥接.NAT. 采用桥接的方法需要占据物理机网段的ip地址,可能会与物理机同一网段的其 ...
- CentOS7:ifconfig command not found解决
https://blog.csdn.net/dandelion_drq/article/details/53503487
- mysql 地理位置定位
SET @pt2 = ST_GeomFromText('POINT(116.405289 39.904987)'); SELECT *,ST_Distance_Sphere(ST_GeomFromTe ...
- ABP框架系列之二十四:(Email-Sending-EF-电子邮件发送)
Introduction Email sending is a pretty common task for almost every application. ASP.NET Boilerplate ...
- Junit 命令行测试 报错:Could not find class 理解及解决方法
一.报错 : 『Could not find class』 下面给出三个示例比较,其中只有第一个是正确的. 1. MyComputer:bin marikobayashi$ java -cp .:./ ...
- solr 高亮springdatasolr
@Autowired private SolrTemplate solrTemplate; @Override public Map<String, Object> search(Map ...
- httpd: apr_sockaddr_info_get() failed for bogon
AH00557: httpd: apr_sockaddr_info_get() failed for bogon AH00558: httpd: Could not reliably determin ...
- 使用js做LeetCode
概述 无意中得知了LeetCode这个刷题网站, 深得我意. 其实作为一个程序员, 我是很看重写基础代码的, 因为这个写熟了, 以后学各种语言就不会太困难. 所以我觉得有必要把这件事记下来, 供以后开 ...
- MongoDB 用法入门(windows)①
概述 大家对数据库肯定不陌生,肯定也有很多人用过MySQL,但是在用MySQL的时候各种建表,写表之间的关联让人非常头疼. MongoDB也是一种数据库,但是它不是用表,而是用集合来装数据的,我对这种 ...