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. Node单线程与异步编程的初步理解

    最近学习了一些关于node的单线程与异步的知识,想拿过来和大家分享下: var async = require('async') //并行无关联,等待事件为最长时间请求过程.如以下两个任务执行时间 c ...

  2. 【c++习题】【17/4/13】stack

    1.stack 模板.动态内存分配.析构 #include "stack2.cpp" #include <iostream> using namespace std; ...

  3. SVN使用—常用命令及避免冲突的方法

    一.SVN启动 [root@localhost ~]# mkdir /data/svn [root@localhost ~]# svnadmin create /data/svn/test [root ...

  4. 2018-2019-2 20165114《网络对抗技术》Exp2 后门原理与实践

    目录 一.实验准备 二.实验内容 三.基础问题回答 四.实验过程及步骤 五.实验总结与体会 六.实验中遇到的问题与错误. 一.实验准备 1.后门概念 后门就是不经过正常认证流程而访问系统的通道. 哪里 ...

  5. 20 个 OpenSSH 最佳安全实践

    来源:https://linux.cn/article-9394-1.html OpenSSH 是 SSH 协议的一个实现.一般通过 scp 或 sftp 用于远程登录.备份.远程文件传输等功能.SS ...

  6. nswag vs swashbuckle

    https://www.reddit.com/r/dotnet/comments/a2181x/swashbuckle_vs_nswag/ Swashbuckle https://github.com ...

  7. 使用VBS打开程序和关闭程序

    下面这个是先执行程序后,然后再结束程序. Dim Wsh Set Wsh = WScript.CreateObject("WScript.Shell") '下行是设置延时启动时间 ...

  8. JSP web.xml <jsp-config>标签使用详解

    <jsp-config> 包括 <taglib> 和 <jsp-property-group> 两个子元素.其中<taglib> 元素在JSP 1.2  ...

  9. js 小复习2

    1.数组 findIndex()  indexOf() // findIndex()方法返回数组中满足提供的测试函数的第一个元素的索引.否则返回-1. function isBigEnough(ele ...

  10. 智课雅思词汇---二十四、名词性后缀ary(也是形容词后缀)

    智课雅思词汇---二十四.名词性后缀ary(也是形容词后缀) 一.总结 一句话总结:很多词缀即是名词词缀也是形容词词缀,很多词即是名词也是形容词 1.名词性后缀-tude? 词根词缀:-tude [来 ...