js获取本月,本季度,上个季度,本周,上周的起始和结束时间

  1 /* 获得某月的天数 */
  2     function getMonthDays(myMonth) {
  3         var nowYear = new Date().getFullYear(); //当前年
  4         var monthStartDate = new Date(nowYear, myMonth, 1);
  5         var monthEndDate = new Date(nowYear, myMonth + 1, 1);
  6         var days = (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24);
  7         return days;
  8     }
  9
 10     /* 获得本周的开始日期 */
 11     function getWeekStartDate() {
 12         var now = new Date();
 13         var nowYear = now.getFullYear(); //当前年
 14         var nowMonth = now.getMonth();
 15         var nowDay = now.getDate();
 16         var nowDayOfWeek = now.getDay(); //今天本周的第几天
 17         var day = nowDay - nowDayOfWeek;
 18         console.log("getWeekStartDate:nowYear=" + nowYear + ",nowMonth=" + nowMonth + ",nowDay=" + nowDay + ",nowDayOfWeek=" + nowDayOfWeek + ",day=" + day);
 19         console.log("getWeekStartDate:" + new Date(nowYear, nowMonth, day));
 20         return new Date(nowYear, nowMonth, day);
 21     }
 22
 23     /* 获得本周的结束日期 */
 24     function getWeekEndDate() {
 25         var now = new Date();
 26         var nowYear = now.getFullYear(); //当前年
 27         var nowMonth = now.getMonth();
 28         var nowDay = now.getDate();
 29         var nowDayOfWeek = now.getDay(); //今天本周的第几天
 30         var day = nowDay + (6 - nowDayOfWeek);
 31         console.log("getWeekEndDate:nowYear=" + nowYear + ",nowMonth=" + nowMonth + ",nowDay=" + nowDay + ",nowDayOfWeek=" + nowDayOfWeek + ",day=" + day);
 32         console.log("getWeekEndDate:" + new Date(nowYear, nowMonth, day));
 33         return new Date(nowYear, nowMonth, day);
 34     }
 35
 36     /* 获得上周的开始日期 */
 37     function getLastWeekStartDate() {
 38         var now = new Date();
 39         var nowYear = now.getFullYear(); //当前年
 40         var nowMonth = now.getMonth();
 41         var nowDay = now.getDate();
 42         var nowDayOfWeek = now.getDay(); //今天本周的第几天
 43         return new Date(nowYear, nowMonth, nowDay - nowDayOfWeek - 7);
 44     }
 45
 46     /* 获得上周的结束日期 */
 47     function getLastWeekEndDate() {
 48         var now = new Date();
 49         var nowYear = now.getFullYear(); //当前年
 50         var nowMonth = now.getMonth();
 51         var nowDay = now.getDate();
 52         var nowDayOfWeek = now.getDay(); //今天本周的第几天
 53         return new Date(nowYear, nowMonth, nowDay - nowDayOfWeek - 1);
 54     }
 55     /* 获得本月的开始日期 */
 56     function getMonthStartDate() {
 57         var now = new Date();
 58         var nowYear = now.getFullYear(); //当前年
 59         var nowMonth = now.getMonth();
 60         return new Date(nowYear, nowMonth, 1);
 61     }
 62
 63     /* 获得本月的结束日期 */
 64     function getMonthEndDate() {
 65         var now = new Date();
 66         var nowYear = now.getFullYear(); //当前年
 67         var nowMonth = now.getMonth();
 68         return new Date(nowYear, nowMonth, getMonthDays(nowMonth));
 69     }
 70
 71     /* 获得上月开始时间 */
 72     function getLastMonthStartDate() {
 73         var lastMonthDate = new Date(); //上月日期
 74         lastMonthDate.setDate(1);
 75         lastMonthDate.setMonth(lastMonthDate.getMonth() - 1);
 76         return lastMonthDate;
 77     }
 78     /* 获得上月结束时间 */
 79     function getLastMonthEndDate() {
 80         var lastMonthDate = new Date(); //上月日期
 81         lastMonthDate.setMonth(lastMonthDate.getMonth() - 1);
 82         var lastMonth = lastMonthDate.getMonth();
 83         lastMonthDate.setDate(getMonthDays(lastMonth));
 84         return lastMonthDate;
 85     }
 86
 87     /* 获得本季度的开始月份 */
 88     function getQuarterStartMonth() {
 89         var nowMonth = new Date().getMonth();
 90         var quarterStartMonth = 0;
 91         if (nowMonth < 3) {
 92             quarterStartMonth = 0;
 93         }
 94         if (2 < nowMonth && nowMonth < 6) {
 95             quarterStartMonth = 3;
 96         }
 97         if (5 < nowMonth && nowMonth < 9) {
 98             quarterStartMonth = 6;
 99         }
100         if (nowMonth > 8) {
101             quarterStartMonth = 9;
102         }
103         return quarterStartMonth;
104     }
105
106     /* 获得本季度的开始日期 */
107     function getQuarterStartDate() {
108         var now = new Date();
109         var nowYear = now.getFullYear(); //当前年
110         return new Date(nowYear, getQuarterStartMonth(), 1);
111     }
112     /* 获得本季度的结束日期 */
113     function getQuarterEndDate() {
114         var now = new Date();
115         var nowYear = now.getFullYear(); //当前年
116         var quarterEndMonth = getQuarterStartMonth() + 2;
117         return new Date(nowYear, quarterEndMonth, getMonthDays(quarterEndMonth));
118     }
119
120     /* 获得上个季度的开始日期 */
121     function getLastQuarterStartDate() {
122         var now = new Date();
123         var nowYear = now.getFullYear(); //当前年
124         return new Date(nowYear, getQuarterStartMonth() - 3, 1);
125     }
126
127     /* 获得上个季度的结束日期 */
128     function getLastQuarterEndDate() {
129         var quarterEndMonth = getQuarterStartMonth() - 1;
130         var now = new Date();
131         now.setMonth(quarterEndMonth);
132         var nowMonth = now.getMonth();
133         var nowYear = now.getFullYear();
134         return new Date(nowYear, quarterEndMonth, getMonthDays(quarterEndMonth));
135     }

UTC时间格式:

2015-05-23T16:00:00.000Z

GMT时间格式:

Sun May 24 2015 00:00:00 GMT+0800 (中国标准时间)

时间: 2024-11-02 02:43:11

js获取本月,本季度,上个季度,本周,上周的起始和结束时间的相关文章

js获取本月、三个月、今年的日期插件dateHelp

最近看了一些关于面向对象的知识,最近工作中在做统计查询的时候需要用到本月.近三个月.今年的日期范围,所以下面用用面向对象的思想写了一个获取日期的插件,大家可以借鉴使用. 直接通过new DateHelp就可以调用了 var myDate = new DateHelp({ date:'2015-02-01',//从此日期开始计算 format:'yyyy/MM/dd'}); myDate.getThisMonth();myDate.getThreeMonth();myDate.getThisYea

moment.js(moment-in-node.js)获取本月最后一天 不指定

http://tommyhu.cn/moment-in-nodejs/ //获取本月最后一天 to=using.moment(日期).endOf('month').format("YYYY-MM-DD")+" 23:59:59"; //获取前一个星期的日期 from=using.moment(日期).add('days', -6).format("YYYY-MM-DD")+" 00:00:00"; // node: var m

js 获取元素在页面上的偏移量的最佳方式

使用js制作效果时,我们常常要获取某个元素在页面上的偏移量(例如tip提示框功能).而获取偏移量可以直接获取相对于document的偏移量,也可以获取相对与视口的偏移量(viewpoint)加上页面滚动量(scroll)获得. 1.获取相对与document的偏移量 function getOffsetSum(ele){ var top= 0,left=0; while(ele){ top+=ele.offsetTop; left+=ele.offsetLeft; ele=ele.offsetP

js 获取input type=&quot;file&quot; 选择的文件大小、文件名称、上次修改时间、类型等信息

<html xmlns="http://www.w3.org/1999/xhtml">   <head runat="server">   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />   <title></title>   </head>   <body&

php 根据给定的年份和月份获取该年份该月份的起始和结束时间

function getShiJianChuo($nian=0,$yue=0){ if(empty($nian) || empty($yue)){ $now = time(); $nian = date("Y",$now); $yue = date("m",$now); } $time['begin'] = mktime(0,0,0,$yue,1,$nian); $time['end'] = mktime(23,59,59,($yue+1),0,$nian); re

jQuery获取当前元素的上一个元素

jQuery获取当前元素的上一个元素: 本章节介绍一下如何利用jquery获取当前元素的下一个元素,如何利用原生js获取下一个元素可以参阅原生js获取当前元素的上一个元素代码实例一章节, 下面直接看代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="author" content="http://www.51texia

C#获取本周、上周、本月、上月、本季度、上季度、本年、上一年起始时间和结束时间

[参考] http://blog.csdn.net/livening/article/details/6049341 http://zhidao.baidu.com/question/378600365.html http://www.cnblogs.com/roy117/archive/2008/03/25/1121584.html 楼上几层的代码都太多了,不用那么复杂. 先跟你说一下原理: 1.国际上的WeekDay是从周日到周六的顺序 2.再说C#的DayOfWeek枚举值是依次从0到6,

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

/** * 获取本周.本季度.本月.上月的开始日期.结束日期 */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

js 获取昨天,今天,本周,上周,季度等时间范围(封装的js)

(function ($, ht) { "use strict"; $.extend(ht, { clickTimeRange:function(){ //点击重置按钮,时间文本框内容清空 $('#btnClear').on('click', function () { $('#selectedInfo').text('') $('.selectInfo ul li').removeClass('selectActive'); $('.selectMenu li').removeCla