/**
* 获取本周、本季度、本月、上月的开始日期、结束日期
*/
var now = new Date(); //当前日期 
var nowDayOfWeek = now.getDay(); //今天本周的第几天 
var nowDay = now.getDate(); //当前日 
var nowMonth = now.getMonth(); //当前月 
var nowYear = now.getYear(); //当前年 
nowYear += (nowYear < 2000) ? 1900 : 0; // var lastMonthDate = new Date(); //上月日期
lastMonthDate.setDate(1);
lastMonthDate.setMonth(lastMonthDate.getMonth()-1);
var lastYear = lastMonthDate.getYear();
var lastMonth = lastMonthDate.getMonth(); //格式化日期:yyyy-MM-dd 
function formatDate(date) { 
var myyear = date.getFullYear(); 
var mymonth = date.getMonth()+1; 
var myweekday = date.getDate();  if(mymonth < 10){ 
mymonth = "0" + mymonth; 

if(myweekday < 10){ 
myweekday = "0" + myweekday; 

return (myyear+"-"+mymonth + "-" + myweekday); 
}  //获得某月的天数 
function getMonthDays(myMonth){ 
var monthStartDate = new Date(nowYear, myMonth, 1); 
var monthEndDate = new Date(nowYear, myMonth + 1, 1); 
var days = (monthEndDate - monthStartDate)/(1000 * 60 * 60 * 24); 
return days; 
}  //获得本季度的开始月份 
function getQuarterStartMonth(){ 
var quarterStartMonth = 0; 
if(nowMonth<3){ 
quarterStartMonth = 0; 

if(2<nowMonth && nowMonth<6){ 
quarterStartMonth = 3; 

if(5<nowMonth && nowMonth<9){ 
quarterStartMonth = 6; 

if(nowMonth>8){ 
quarterStartMonth = 9; 

return quarterStartMonth; 
}  //获得本周的开始日期 
function getWeekStartDate() { 
var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek); 
return formatDate(weekStartDate); 
}  //获得本周的结束日期 
function getWeekEndDate() { 
var weekEndDate = new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek)); 
return formatDate(weekEndDate); 
}  //获得本月的开始日期 
function getMonthStartDate(){ 
var monthStartDate = new Date(nowYear, nowMonth, 1); 
return formatDate(monthStartDate); 
}  //获得本月的结束日期 
function getMonthEndDate(){   var days= getMonthDays(nowMonth);//获取当月总共有多少天
 var monthEndDate = new Date(nowYear, nowMonth, days); 
 return formatDate(monthEndDate); //返回当月结束时间
} 注意:因为上面这个方法里的getMonthDays(nowMonth)方法 我尝试使用失败,我不知道是什么原因 所以我用了另外个方法如下(获得某月的天数,需要配合文章开头的公式 ): //获得某月的天数 (与上面有重复可删除,不然本月结束日期报错)
    function getMonthDays(nowyear,myMonth){
     var lastMonthStartDate = new Date(nowyear, lastMonth, 1);
     var lastMonthEndDate= new Date(nowyear, lastMonth+ 1, 1);
     var days = (lastMonthEndDate- lastMonthStartDate) / (1000 * 60 * 60 * 24);//格式转换
    alert(days);
   } //获得上月开始时间
function getLastMonthStartDate(){
var lastMonthStartDate = new Date(nowYear, lastMonth, 1);
return formatDate(lastMonthStartDate); 
} //获得上月结束时间
function getLastMonthEndDate(){
var lastMonthEndDate = new Date(nowYear, lastMonth, getMonthDays(lastMonth));
return formatDate(lastMonthEndDate); 
} //获得本季度的开始日期 
function getQuarterStartDate(){  var quarterStartDate = new Date(nowYear, getQuarterStartMonth(), 1); 
return formatDate(quarterStartDate); 
}  //或的本季度的结束日期 
function getQuarterEndDate(){ 
var quarterEndMonth = getQuarterStartMonth() + 2; 
var quarterStartDate = new Date(nowYear, quarterEndMonth, getMonthDays(quarterEndMonth)); 
return formatDate(quarterStartDate); 
} //获取本年开始-当前时间 var currentYear=now.getFullYear();//获得当前年份4位年 var currentYearFirstDate=new Date(currentYear,0,1); //本年第一天 var startTime = currentYearFirstDate.getFullYear() + '-' + (currentYearFirstDate.getMonth() +1) + '-' + currentYearFirstDate.getDate()+''+currentYearFirstDate.getHours()+':'+currentYearFirstDate.getMinutes()+':'+ currentYearFirstDate.getSeconds(); //格式化本年第一天日期 var currentYearEndDate=now; //当前时间

JS获取本周、本季度、本月、上月、本年的开始日期、结束日期的更多相关文章

  1. js 获取 本周、上周、本月、上月、本季度、上季度的开始结束日期

    js 获取 本周.上周.本月.上月.本季度.上季度的开始结束日期 /**  * 获取本周.本季度.本月.上月的开始日期.结束日期  */ var now = new Date(); //当前日期 va ...

  2. JS获取本周、本季度、本月、上月的开始日期、结束日期

    /** * 获取本周.本季度.本月.上月的开始日期.结束日期 */ var now = new Date();                    //当前日期 var nowDayOfWeek = ...

  3. php 获取今日、昨日、本周,上周、本月,上月,季度的起始时间戳和结束时间戳的方法

    php 获取今日.昨日.上周.本月的起始时间戳和结束时间戳的方法,主要使用到了 php 的时间函数 mktime.下面首先还是直奔主题以示例说明如何使用 mktime 获取今日.昨日.上周.本月的起始 ...

  4. moment.js获取本周本月本年的开始日期和结束日期

    //获取本日 const startDate = moment().format('YYYY-MM-DD'); const startDate = moment().format('YYYY-MM-D ...

  5. Asp.net C# 获取本周上周本月上月本年上年第一天最后一天时间大全

    DateTime dt = DateTime.Now; int weeknow = Convert.ToInt32(DateTime.Now.DayOfWeek); ) * weeknow + ; D ...

  6. js获取本周、上周的开始结束时间

    这两天在做一个报表体统,其中涉及到了一个根据本周,上周,本月,上月的时间来进行查询的问题,在这个我就教一下大家怎么实现,大家如果有更好的实现方法的,我也希望大家能说出来,我们交流交流. 首先呢,我写了 ...

  7. js获取本周日期

    JS获取到本周的日期 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> &l ...

  8. js获得本季度的开始日期 结束日期

    var now = new Date(); //当前日期var nowMonth = now.getMonth()+1; //当前月var nowYear = now.getFullYear(); / ...

  9. JS 获取 本周、本月、本季度、本年、上月、上周、上季度、去年

    工具类定义: /** * 日期范围工具类 */ var dateRangeUtil = (function () { /*** * 获得当前时间 */ this.getCurrentDate = fu ...

随机推荐

  1. redis单机多节点集群

    # ##安装Redis redis安装参考 https://www.cnblogs.com/renxixao/p/11442770.html Reids安装包里有个集群工具,要复制到/usr/loca ...

  2. 【C语言】 strlen()入参空指针导致段错误

    背景: 在工作中调试sqlite3相关代码的时候,调用printf()打印sqlite3_exec()的执行日志:因为sqlite3_exec()保存日志的参数传入时为NULL,且没有执行错误,所以再 ...

  3. QT 学习基础问题记录

    1. connect 函数 需要先创建发送者和接收者实例,并且信号函数和槽函数如果有参数,需要在 connect 函数使用时指定相关参数类型. 2.窗口控件设置 设置窗口的最大化.最小化.问号提示等控 ...

  4. 洛谷--P3808 【模板】AC自动机(“假的“简单版)

    如果你想要做出这道题,你需要先了解两个知识点: 1.字典树的构造 2.KMP算法(也就是fail指针的构造) 对于字典树,可以看看这个大佬: https://www.cnblogs.com/TheRo ...

  5. 小白的C++之路——结构体

    结构体与数组不同的是结构体能包含不同类型的数据,像一个综合性的仓库,更加灵活. #include <iostream> #include <string> #include & ...

  6. 线程池及Executor框架

    1.为什么要使用线程池? ​ 诸如 Web 服务器.数据库服务器.文件服务器或邮件服务器之类的许多服务器应用程序都面向处理来自某些远程来源的大量短小的任务.请求以某种方式到达服务器,这种方式可能是通过 ...

  7. node-red inject节点 debug节点 switch节点

    inject节点: https://blog.csdn.net/geek_monkey/article/details/80737818 debug节点: https://blog.csdn.net/ ...

  8. easy ui 弹框叠加问题

    1.框架用的是.net MVC,Index页面如下所示: @{ Layout = "~/Views/Shared/_CustomerLayout.cshtml"; ViewBag. ...

  9. MySQL中的JSON类型

    前言(废话) 昨天抽了点时间在网上搜列了一个开源项目,项目挺完整的,前后台分离还带有微信小程序,我Clone下代码,经过一番倒腾,嘿~还真就跑起来了.在这个过程中,体验了一把VUE项目工程细节,因为之 ...

  10. 使用SAP Cloud Application Programming模型开发OData的一个实际例子

    刚刚过去的SAP TechEd上,SAP CTO Juergen Mueller向外界传递了一个重要的信息:身处云时代大环境下的SAP从业者,在SAP云平台上该如何选择适合自己的开发方式? Juerg ...