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. rpm命令,yum命令大全

    rpm 执行安装包二进制包(Binary)以及源代码包(Source)两种.二进制包可以直接安装在计算机中,而源代码包将会由RPM自动编译.安装.源代码包经常以src.rpm作为后缀名. 常用命令组合 ...

  2. 防火墙之地址转换SNAT DNAT

    一.SNAT源地址转换. 1.原理:在路由器后(PSOTROUTING)将内网的ip地址修改为外网网卡的ip地址. 2.应用场景:共享内部主机上网. 3.设置SNAT:网关主机进行设置. (1)设置i ...

  3. JavaScript的消息机制

    JavaScript本身是单线程的,但它却是事件驱动的.类似Windows窗体应用程序,它也需要消息队列机制来实现.程序的执行并不是连续的,绝大多数时间都在等待消息.每次执行执行程序都是在响应消息,这 ...

  4. 基于Bootstrap的日历控件和日期选择插件

    在线演示 本地下载

  5. [mongodb] WiredTiger Storage Engine

    今天看了mongodb的官方文档中的WiredTiger Storage Engine ,说说我对WiredTiger Storage Engine 的理解! 在mongodb3.2版本以后,wire ...

  6. php上传文件后无法移动到指定目录的解决

    从浏览器访问而触发PHP脚本运行的用户是 apache 用户 无法移动文件的原因主要是目标目录没有写入权限 1.将目标目录权限设置为 777 #chmod 777 tar_dir 2.将目标目录用户和 ...

  7. JS 闭包应用

    1. 代替全局变量 //闭包应用1:代替全局变量的使用 //多个函数都用到一个变量,通常我们会定义一个全局变量,然后在各函数中应用它,//为了避免使用全局变量,可以通过使用立即执行函数定义临时变量,子 ...

  8. Java编程时部分快捷键

    alt + / 内容助理 配置:Window->properties->keys->查看alt + /的配置,然后解除当前的配置->搜索content assist->解 ...

  9. Count and Say,统计并输出,利用递归,和斐波那契数列原理一样。

    问题描述:n=1,返回“1”:n=2,返回“11”:n=3,返回“21”:n=4,返回1211,.... 算法分析:和斐波那契数列道理差不多,都是后一个要依赖前一个元素.因此可以使用递归,也可以使用迭 ...

  10. python函数式编程之高阶函数学习

    基本概念 函数式编程,是一种抽象程度很高的编程范式,纯粹的函数式编程语言编写的函数没有变量.因此,任意一个函数,只要输入确定,输出就确定的这种函数我们称之为纯函数,我们称这种函数没有副作用.而允许使用 ...