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. Linux中显示空闲内存空间的free命令的基本用法

    free 命令显示系统使用和空闲的内存情况,包括物理内存.交互区内存(swap)和内核缓冲区内存 参数 -b 显示内存的单位为字节-k 显示内存的单位为 KB-m 显示内存的单位为 M-o 忽略缓冲区 ...

  2. CentOS 7 安装各个桌面版本

    http://unix.stackexchange.com/questions/181503/how-to-install-desktop-environments-on-centos-7 92dow ...

  3. 读取Excel复杂的数据

    涉及到合并单元格的数据读取: package com.util; import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.util ...

  4. HDU 1337 && POJ 1218&& zju 1350 方法总结

    题目链接http://acm.hdu.edu.cn/showproblem.php?pid=1337 杭电 http://poj.org/problem?id=1218清华 http://acm.zj ...

  5. spring security使用哈希加密的密码

    之前我们都是使用MD5 Md5PasswordEncoder 或者SHA ShaPasswordEncoder 的哈希算法进行密码加密,在spring security中依然使用只要指定使用自定义加密 ...

  6. CODE FESTIVAL 2015 決勝(部分)

    CODE FESTIVAL 2015 決勝(部分) B - ダイスゲーム 题意: 给\(N\)个\(6\)个面骰子,然后问掷到概率最大的点数是多少. 分析: 随便打表随便发现是\(\huge\left ...

  7. merge two sorted lists, 合并两个有序序列

    /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * Lis ...

  8. 给定字符串数组,用map的key保存数组中字符串元素,value保存字符串元素出现次数,最后统计个字符串元素出现次数

    import java.util.HashMap; public class map1 { public static void main(String[] args) { String[] arra ...

  9. Python 导出数据from Mysql

    环境 Anaconda3 Python 3.6, Window 64bit 目的 从MySQL数据库读取目标表数据,并处理 代码 # -*- coding: utf-8 -*- import pand ...

  10. python 爬虫004-使用urllib2与正则表达式扒取糗事百科新鲜页首页帖子

    面向过程的方式 #!/usr/bin/env python # -*- coding: utf-8 -*- import urllib2 import sys import re import os ...