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 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获取本周、本季度、本月、上月的开始日期、结束日期的更多相关文章
- js 获取 本周、上周、本月、上月、本季度、上季度的开始结束日期
js 获取 本周.上周.本月.上月.本季度.上季度的开始结束日期 /** * 获取本周.本季度.本月.上月的开始日期.结束日期 */ var now = new Date(); //当前日期 va ...
- JS获取本周、本季度、本月、上月、本年的开始日期、结束日期
/** * 获取本周.本季度.本月.上月的开始日期.结束日期 */ var now = new Date(); //当前日期 var nowDayOfWeek = now.getDay(); //今 ...
- php 获取今日、昨日、本周,上周、本月,上月,季度的起始时间戳和结束时间戳的方法
php 获取今日.昨日.上周.本月的起始时间戳和结束时间戳的方法,主要使用到了 php 的时间函数 mktime.下面首先还是直奔主题以示例说明如何使用 mktime 获取今日.昨日.上周.本月的起始 ...
- moment.js获取本周本月本年的开始日期和结束日期
//获取本日 const startDate = moment().format('YYYY-MM-DD'); const startDate = moment().format('YYYY-MM-D ...
- js获取本周、上周的开始结束时间
这两天在做一个报表体统,其中涉及到了一个根据本周,上周,本月,上月的时间来进行查询的问题,在这个我就教一下大家怎么实现,大家如果有更好的实现方法的,我也希望大家能说出来,我们交流交流. 首先呢,我写了 ...
- js获取本周日期
JS获取到本周的日期 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> &l ...
- js获得本季度的开始日期 结束日期
var now = new Date(); //当前日期var nowMonth = now.getMonth()+1; //当前月var nowYear = now.getFullYear(); / ...
- PHP获取一年有多少周和每周开始和结束日期
/*PHP获取当前日期是第几周和本周开始日期和本周结束日期*/ //$now = '2018-11-13';周二 public function getNowTimeInfo($now) { $str ...
- JS 获取 本周、本月、本季度、本年、上月、上周、上季度、去年
工具类定义: /** * 日期范围工具类 */ var dateRangeUtil = (function () { /*** * 获得当前时间 */ this.getCurrentDate = fu ...
随机推荐
- 基于vue2.0实现仿百度前端分页效果(二)
前言 上篇文章中,已经使用vue实现前端分页效果,这篇文章我们单独将分页抽离出来实现一个分页组件 先看实现效果图 代码实现 按照惯例,我们在冻手实现的时候还是先想一想vue实现组件的思路 1.需要提前 ...
- Java基础系列--final、finally关键字
原创作品,可以转载,但是请标注出处地址:http://www.cnblogs.com/V1haoge/p/8482909.html 一.概述 final是Java关键字中最常见之一,表示“最终的,不可 ...
- 【学习笔记】剖析MVVM框架,简单实现Vue数据双向绑定
前言: 学习前端也有半年多了,个人的学习欲望还比较强烈,很喜欢那种新知识在自己的演练下一点点实现的过程.最近一直在学vue框架,像网上大佬说的,入门容易深究难.不管是跟着开发文档学还是视频教程,按步骤 ...
- BGP笔记
BGP:用于AS与AS之间的路由,但现在也越来越多的用在IDC内部了 BGP是应用层协议,应用TCP协议(唯一一个运用TCP的路由协议) IGP和EGP的区别:IGP运行在一个AS之内,EGP运行在A ...
- 对于Ext.data.Store 介紹 与总结,以及对以前代码的重构与优化
对于Ext.data.Store 一直不是很了解,不知道他到底是干嘛的有哪些用处,在实际开发中也由于不了解也走了不少弯路, store是一个为Ext器件提供record对象的存储容器,行为和属性都很象 ...
- vue 前台传后台
var the = this:let url = "/api/Purchase_Enter/CancelEnter"; let params = { Enter_Id: Enter ...
- c# 虚拟路径转换为绝对路径
/// <summary> /// 解析相对Url /// </summary> /// <param name="relativeUrl">相 ...
- LeetCode两数之和-Python<一>
下一篇:LeetCode链表相加-Python<二> 题目:https://leetcode-cn.com/problems/two-sum/description/ 给定一个整数数组和一 ...
- java Future用法和意义一句话击破
在并发编程时,一般使用runnable,然后扔给线程池完事,这种情况下不需要线程的结果. 所以run的返回值是void类型. 如果是一个多线程协作程序,比如菲波拉切数列,1,1,2,3,5,8...使 ...
- HTML5 template元素
前言 转自http://www.zhangxinxu.com/wordpress/2014/07/hello-html5-template-tag/ 在单页面应用,我们对页面的无刷新有了更高的要求,H ...