1. /**
  2. * 获取本周、本季度、本月、上月的开始日期、结束日期
  3. */
  4. var now = new Date();                    //当前日期
  5. var nowDayOfWeek = now.getDay();         //今天本周的第几天
  6. var nowDay = now.getDate();              //当前日
  7. var nowMonth = now.getMonth();           //当前月
  8. var nowYear = now.getYear();             //当前年
  9. nowYear += (nowYear < 2000) ? 1900 : 0;  //
  10. var lastMonthDate = new Date();  //上月日期
  11. lastMonthDate.setDate(1);
  12. lastMonthDate.setMonth(lastMonthDate.getMonth()-1);
  13. var lastYear = lastMonthDate.getYear();
  14. var lastMonth = lastMonthDate.getMonth();
  15. //格式化日期:yyyy-MM-dd
  16. function formatDate(date) {
  17. var myyear = date.getFullYear();
  18. var mymonth = date.getMonth()+1;
  19. var myweekday = date.getDate();
  20. if(mymonth < 10){
  21. mymonth = "0" + mymonth;
  22. }
  23. if(myweekday < 10){
  24. myweekday = "0" + myweekday;
  25. }
  26. return (myyear+"-"+mymonth + "-" + myweekday);
  27. }
  28. //获得某月的天数
  29. function getMonthDays(myMonth){
  30. var monthStartDate = new Date(nowYear, myMonth, 1);
  31. var monthEndDate = new Date(nowYear, myMonth + 1, 1);
  32. var   days   =   (monthEndDate   -   monthStartDate)/(1000   *   60   *   60   *   24);
  33. return   days;
  34. }
  35. //获得本季度的开始月份
  36. function getQuarterStartMonth(){
  37. var quarterStartMonth = 0;
  38. if(nowMonth<3){
  39. quarterStartMonth = 0;
  40. }
  41. if(2<nowMonth && nowMonth<6){
  42. quarterStartMonth = 3;
  43. }
  44. if(5<nowMonth && nowMonth<9){
  45. quarterStartMonth = 6;
  46. }
  47. if(nowMonth>8){
  48. quarterStartMonth = 9;
  49. }
  50. return quarterStartMonth;
  51. }
  52. //获得本周的开始日期
  53. function getWeekStartDate() {
  54. var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek);
  55. return formatDate(weekStartDate);
  56. }
  57. //获得本周的结束日期
  58. function getWeekEndDate() {
  59. var weekEndDate = new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek));
  60. return formatDate(weekEndDate);
  61. }
  62. //获得本月的开始日期
  63. function getMonthStartDate(){
  64. var monthStartDate = new Date(nowYear, nowMonth, 1);
  65. return formatDate(monthStartDate);
  66. }
  67. //获得本月的结束日期
  68. function getMonthEndDate(){
  69. var monthEndDate = new Date(nowYear, nowMonth, getMonthDays(nowMonth));
  70. return formatDate(monthEndDate);
  71. }
  72. //获得上月开始时间
  73. function getLastMonthStartDate(){
  74. var lastMonthStartDate = new Date(nowYear, lastMonth, 1);
  75. return formatDate(lastMonthStartDate);
  76. }
  77. //获得上月结束时间
  78. function getLastMonthEndDate(){
  79. var lastMonthEndDate = new Date(nowYear, lastMonth, getMonthDays(lastMonth));
  80. return formatDate(lastMonthEndDate);
  81. }
  82. //获得本季度的开始日期
  83. function getQuarterStartDate(){
  84. var quarterStartDate = new Date(nowYear, getQuarterStartMonth(), 1);
  85. return formatDate(quarterStartDate);
  86. }
  87. //或的本季度的结束日期
  88. function getQuarterEndDate(){
  89. var quarterEndMonth = getQuarterStartMonth() + 2;
  90. var quarterStartDate = new Date(nowYear, quarterEndMonth, getMonthDays(quarterEndMonth));
  91. return formatDate(quarterStartDate);
  92. }

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

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

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

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

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

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

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

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

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

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

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

  6. js获取本周日期

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

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

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

  8. PHP获取一年有多少周和每周开始和结束日期

    /*PHP获取当前日期是第几周和本周开始日期和本周结束日期*/ //$now = '2018-11-13';周二 public function getNowTimeInfo($now) { $str ...

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

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

随机推荐

  1. 基于vue2.0实现仿百度前端分页效果(二)

    前言 上篇文章中,已经使用vue实现前端分页效果,这篇文章我们单独将分页抽离出来实现一个分页组件 先看实现效果图 代码实现 按照惯例,我们在冻手实现的时候还是先想一想vue实现组件的思路 1.需要提前 ...

  2. Java基础系列--final、finally关键字

    原创作品,可以转载,但是请标注出处地址:http://www.cnblogs.com/V1haoge/p/8482909.html 一.概述 final是Java关键字中最常见之一,表示“最终的,不可 ...

  3. 【学习笔记】剖析MVVM框架,简单实现Vue数据双向绑定

    前言: 学习前端也有半年多了,个人的学习欲望还比较强烈,很喜欢那种新知识在自己的演练下一点点实现的过程.最近一直在学vue框架,像网上大佬说的,入门容易深究难.不管是跟着开发文档学还是视频教程,按步骤 ...

  4. BGP笔记

    BGP:用于AS与AS之间的路由,但现在也越来越多的用在IDC内部了 BGP是应用层协议,应用TCP协议(唯一一个运用TCP的路由协议) IGP和EGP的区别:IGP运行在一个AS之内,EGP运行在A ...

  5. 对于Ext.data.Store 介紹 与总结,以及对以前代码的重构与优化

    对于Ext.data.Store 一直不是很了解,不知道他到底是干嘛的有哪些用处,在实际开发中也由于不了解也走了不少弯路, store是一个为Ext器件提供record对象的存储容器,行为和属性都很象 ...

  6. vue 前台传后台

    var the = this:let url = "/api/Purchase_Enter/CancelEnter"; let params = { Enter_Id: Enter ...

  7. c# 虚拟路径转换为绝对路径

    /// <summary> /// 解析相对Url /// </summary> /// <param name="relativeUrl">相 ...

  8. LeetCode两数之和-Python<一>

    下一篇:LeetCode链表相加-Python<二> 题目:https://leetcode-cn.com/problems/two-sum/description/ 给定一个整数数组和一 ...

  9. java Future用法和意义一句话击破

    在并发编程时,一般使用runnable,然后扔给线程池完事,这种情况下不需要线程的结果. 所以run的返回值是void类型. 如果是一个多线程协作程序,比如菲波拉切数列,1,1,2,3,5,8...使 ...

  10. HTML5 template元素

    前言 转自http://www.zhangxinxu.com/wordpress/2014/07/hello-html5-template-tag/ 在单页面应用,我们对页面的无刷新有了更高的要求,H ...