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. Python面试题之容器(Collections)

    容器(Collections) Python附带一个模块,它包含许多容器数据类型,名字叫作collections.我们将讨论它的作用和用法.   我们将讨论的是:   defaultdict coun ...

  2. 无缝走A的终极技巧:学会了你也是走A怪

    ADC重点之中:改键铸造更强走A! 我们先来欣赏一波来自世界顶尖ADC大师兄Doublelift的教科书般的走A. 他使用的金克丝在空蓝的情况下,凭借娴熟的走A技巧,拿下五杀. 关于走A,其实它有着一 ...

  3. 部署私有云网盘owncloud

    环境说明: [root@localhost ~]# cat /etc/redhat-release CentOS release 6.9 (Final) [root@localhost ~]# una ...

  4. COS-8文件系统

    操作系统(Operating System,简称OS)是管理和控制计算机硬件与软件资源的计算机程序,是直接运行在“裸机”上的最基本的系统软件,任何其他软件都必须在操作系统的支持下才能运行.   操作系 ...

  5. jq限制字符个数

    <script> $(document).ready(function () { //限制字符个数 $(".box-right .title a").each(func ...

  6. java 实现图片拼接

    1.想要实现多个图片的纵向和横向拼接.实现代码如下 2.补充发现横向拼接的一个bug 宽度没有增加(已修复) 百度地址:工具jar下载 链接:https://pan.baidu.com/s/1uZCN ...

  7. 机器学习三剑客之Pandas

      pandas Pandas是基于Numpy开发出的,专门用于数据分析的开源Python库 Pandas的两大核心数据结构 Series(一维数据)   Series   创建Series的方法   ...

  8. kolla all-in-one 安装

    http://docs.openstack.org/developer/kolla/ 使用了Docker containers and Ansible playbooks 目前在Fedora/Ubun ...

  9. MSSQL2005数据库显示单一用户模式,无法进行任何操作

    MSSQL2005数据库显示单一用户模式,无法进行任何操作 经查询,使用exec sp_who进行查看链接线程,发现仍然有链接不断进行请求,将链接踢出,然后通过命令修复即可恢复 处理步骤: exec ...

  10. 分享:自定义JAVA注解

    元注解 元注解指用来定义注解的注解,例如:@Retention @Target Inherited @Documented等等.最为重要和经常使用的是@Retention @Target. @Rete ...