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. linux nohup

    nohup RAILS_ENV=production bundle exec XXXX & nohup RAILS_ENV=production bundle exec XXXX >/d ...

  2. Python程序每日一练习

    问题一:做为Apple Store App独立开发者,你要搞限时促销,为你的应用生成激活码(或者优惠券),使用Python如何生成200个激活码(或者优惠券)? 简介:通用唯一识别码(英语:Unive ...

  3. Maven教程1(介绍安装和配置)

    官网地址:http://maven.apache.org/ 1.Maven介绍 1.1为什么需要使用Maven 之前学Spring和SpringMVC的时候我们需要单独自己去找相关的jar. 这些ja ...

  4. Java设计模式学习记录-桥接模式

    前言 这次介绍结构型设计模式中的第二种模式,桥接模式. 使用桥接模式的目的就是为了解耦,松散的耦合更利于扩展,但是会增加相应的代码量和设计难度. 桥接模式 桥接模式是为了将抽象化与实现化解耦,让二者可 ...

  5. module.exports和exports.md

    推荐写法 具体解释可以往后看. 'use strict' let app = { // 注册全局对象 ... } ... // 封装工具箱 exports = module.exports = app ...

  6. MEF 插件式开发之 小试牛刀

    MEF 简介 Managed Extensibility Framework 即 MEF 是用于创建轻量.可扩展应用程序的库. 它让应用程序开发人员得以发现和使用扩展且无需配置. 它还让扩展开发人员得 ...

  7. 微信小程序开发BUG经验总结

    摘要: 常见的微信小程序BUG! 小程序开发越来越热,开发中遇到各种各样的bug,在此总结了一些比较容易掉进去的坑分享给大家. 1. new Date跨平台兼容性问题 在Andriod使用new Da ...

  8. 安装 kubernetes v1.11.1

    kubernetes 版本 v1.11.1 系统版本:Centos 7.4 3.10.0-693.el7.x86_64 master: 192.168.0.205 node1: 192.168.0.2 ...

  9. python中集合-set

    集合-set 集合是高中数学中的一个概念 一堆确定的无序的唯一的数据,集合中每一个数据成为一个元素 # 集合的定义 s = set() print(type(s)) print(s) print(&q ...

  10. 禁用 Gnome Shell 默认的 Ubuntu Dock 和 Ubuntu AppIndicators 扩展

    以前折腾的时候禁用过,现在已经忘记目录了,结果今天手贱把系统从 18.04 升级到了 18.10 ,很多东西都要重新搞过,而且用惯了 mac 已经不熟悉 linux 上瞎折腾的那一套了,简直坑爹.. ...