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; //

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 getLastWeekStartDate() {

    var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek - 7);

    return formatDate(weekStartDate);

}

//获得上周的结束日期

function getLastWeekEndDate() {

    var weekEndDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek - 1);

    return formatDate(weekEndDate);

}

//获得本月的开始日期

function getMonthStartDate() {

    var monthStartDate = new Date(nowYear, nowMonth, 1);

    return formatDate(monthStartDate);

}

//获得本月的结束日期

function getMonthEndDate() {

    var monthEndDate = new Date(nowYear, nowMonth, getMonthDays(nowMonth));

    return formatDate(monthEndDate);

}

//获得上月开始时间

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);

}

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

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

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

  2. 用php获取本周,上周,本月,上月,本季度日期的代码

    echo date("Ymd",strtotime("now")), "\n"; echo date("Ymd",str ...

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

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

  4. [转] Js获取 本周、本月、本季度、本年、上月、上周、上季度、去年时间段

    /** * 针对时间的工具类 */ var DateTimeUtil = function () { /*** * 获得当前时间 */ this.getCurrentDate = function ( ...

  5. JS 时间 获取 当天,昨日,本周,上周,本月,上月

    调用 setTimeRange (2); function  setTimeRange (v) { var fmt = 'YYYY-MM-DD HH:mm'; var now = new Date() ...

  6. JavaScript(js)获取本周,本月,本季,本年,上月,上周,上季,去年,上二周,上二月的时间段的代码

    function dateChange(name){ var beginTimeObject = document.getElementById("beginTime"); var ...

  7. Js 获取 本周、本月起始时间

    涉及到显示本月或本周相关信息,又不想让php去判断,只好直接用js去计算,麻烦了好一阵,还是老老实实的看了下js的日期函数.现总结一下: //计算本周起始日期,并以 Y-m-d 形式返回.    fu ...

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

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

  9. [moka同学笔记]php 获取时间(今天,昨天,三天内,本周,上周,本月,三年内,半年内,一年内,三年内)

    <?php /** * php 获取时间(今天,昨天,三天内,本周,上周,本月,三年内,半年内,一年内,三年内) * * author:ihelloworld2010@gmail.com * d ...

随机推荐

  1. poj3125

    /*水题,模拟排队*/#include<stdio.h>#include<string.h>#include<algorithm>using namespace s ...

  2. Linux网络IO函数以及TCP连接函数包装

    标准I/O VS 网络IO 标准I/O又称为标准I/O流,从某种意义上讲是全双工的,因为程序能够在同一个流上执行输入和输出. Unix/Linux对网络的抽象是一种称为套接字的文件类型.和任何Unix ...

  3. TRUNC函数的用法

    TRUNC函数用于对值进行截断. 用法有两种:TRUNC(NUMBER)表示截断数字,TRUNC(date)表示截断日期. (1)截断数字: 格式:TRUNC(n1,n2),n1表示被截断的数字,n2 ...

  4. 使用shiro缓存用户身份信息的时候报:java.io.NotSerializableException: org.apache.shiro.util.SimpleByteSource

    最近在使用shiro缓存用户的身份信息的时候,报了simpleByteSource不能序列化,跟进源码一看,原来这个类没有实现序列化的接口,但是我在缓存身份信息的实现又要用到这个类,解决方法:重写一个 ...

  5. 图论_FatherChristmasFlymouse(Tarjan+dijkstra or spfa)

    堆优化Dij VS Spfa 堆优化Dij小胜一筹. 题目名字:Father Christmas flymouse (POJ 3160) 这题可以说是图论做的比较畅快的一题,比较综合,很想说一说. 首 ...

  6. LeetCode第[62]题(Java):Unique Paths 及扩展

    题目:唯一路径(机器人走方格) 难度:Medium 题目内容: A robot is located at the top-left corner of a m x n grid (marked 'S ...

  7. tensorflow笔记:模型的保存与训练过程可视化

    tensorflow笔记系列: (一) tensorflow笔记:流程,概念和简单代码注释 (二) tensorflow笔记:多层CNN代码分析 (三) tensorflow笔记:多层LSTM代码分析 ...

  8. lightoj1370欧拉函数/素数筛

    这题有两种解法,1是根据欧拉函数性质:素数的欧拉函数值=素数-1(可根据欧拉定义看出)欧拉函数定义:小于x且与x互质的数的个数 #include<map> #include<set& ...

  9. Xcode删除无用的Symbols信息

    open ~/Library/Developer/Xcode/iOS\ DeviceSupport 进入后对不需要的版本手动Delete.

  10. Hash算法-CityHash算法

    cityhash系列字符串散列算法是由著名的搜索引擎公司Google 发布的 (http://www.cityhash.org.uk/). Google发布的有两种算法:cityhash64 与 ci ...