1. 程式人生 > >JS獲取本週、本季度、本月、上月的開端日期、停止日期

JS獲取本週、本季度、本月、上月的開端日期、停止日期

var getdate=function(){
    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 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;
    }
    
    //獲得本週的開端日期
    this.getWeekStartDate=function () {
        var weekStartDate;
        if(nowDayOfWeek==0){
            weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek+1-7);
        }else{
            weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek+1);
        }
        return formatDate(weekStartDate);
    }
    //獲得本週的停止日期
    this.getWeekEndDate=function() {
        var weekEndDate;
        if(nowDayOfWeek==0){
            weekEndDate = new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek)+1-7);
        }else{
            weekEndDate = new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek)+1);
        }
        return formatDate(weekEndDate);
    }
    //獲得本月的開端日期
    this.getMonthStartDate=function(){
        var monthStartDate = new Date(nowYear, nowMonth, 1);
        return formatDate(monthStartDate);
    }
    //獲得本月的停止日期
    this.getMonthEndDate=function(){
        var monthEndDate = new Date(nowYear, nowMonth, getMonthDays(nowMonth));
        return formatDate(monthEndDate);
    }
    //獲得上月開端時候
    this.getLastMonthStartDate=function(){
        var lastMonthStartDate = new Date(nowYear, lastMonth, 1);
        return formatDate(lastMonthStartDate);
    }
    //獲得上月停止時候
    this.getLastMonthEndDate=function(){
        var lastMonthEndDate = new Date(nowYear, lastMonth, getMonthDays(lastMonth));
        return formatDate(lastMonthEndDate);
    }
    //獲得本季度的開端日期
    this.getQuarterStartDate=function(){
        var quarterStartDate = new Date(nowYear, getQuarterStartMonth(), 1);
        return formatDate(quarterStartDate);
    }
    //獲得本季度的停止日期
    this.getQuarterEndDate=function(){
        var quarterEndMonth = getQuarterStartMonth() + 2;
        var quarterStartDate = new Date(nowYear, quarterEndMonth, getMonthDays(quarterEndMonth));
        return formatDate(quarterStartDate);
    }
}
//使用方法
var mydate=new getdate();
alert('本週時間: '+mydate.getWeekStartDate()+'--'+mydate.getWeekEndDate());

相關推薦

JS獲取本週季度本月上月本年的開始日期結束日期

/** * 獲取本週、本季度、本月、上月的開始日期、結束日期 */ var now = new Date(); //當前日期  var nowDayOfWeek = now.getDay(); //今天本週的第幾天  var nowDay = now.getDate(); //當

JS日期轉換格式 和 JS 獲取 今日昨日本週本月季度本年上月上週季度去年

// 對Date的擴充套件,將 Date 轉化為指定格式的String // 月(M)、日(d)、小時(h)、分(m)、秒(s)、季度(q) 可以用 1-2 個佔位符, // 年(y)可以用 1-4 個佔位符,毫秒(S)只能用 1 個佔位符(是 1-3 位的數字) // 例子: // (new D

JS 獲取 本週本月季度本年上月上週季度去年

/** * 日期範圍工具類 */ var dateRangeUtil = (function () { /*** * 獲得當前時間 */ this.getCurrentDate = function () { return ne

JS獲取本週週一 週日日期季度本月上月開端日期停止日期

1、首先來一個自己公司專案的原始碼: 專案需求描述: 從20150712日開始, , 需求①:根據當前時間返回每一週 、週一~週日的日期(需返回2種格式 格式1:7月13日,格式2:2015-07-13) 需求②:返回當前時間本週的,週一早上9:00:00和週日的23:59

JS獲取本週季度本月上月開端日期停止日期

var getdate=function(){ var now = new Date(); //當前日期 var nowDayOfWeek = now.getDay(); //今天本週的第幾天 var nowDay = now.getDate();

獲取(js)本週週一 週日日期季度本月上月開端日期停止日期(轉帖)...

轉帖地址:http://blog.csdn.net/kongjiea/article/details/46829023 1、首先來一個自己公司專案的原始碼: 專案需求描述: 從20150712日開始, , 需求①:根據當前時間返回每一週 、週一~週日的日期(需返回2種格式 格式1:7月13日,格式2:201

php 獲取時間(今天,昨天,三天內,本週,上週,本月,三年內,半年內,一年內,三年內) PHP獲取上週本週上月本月季度季度時間方法大全

<?php date_default_timezone_set('PRC'); /** * 獲取最近一週,一個月,一年 * */ function getLatelyTime($type = ''){ $now = time(); $result = []; if

PHP獲取昨天今天上週本週上月本月季度季度今年的起始時間

//今天開始時間 $beginToday= date("Y-m-d H:i:s",mktime(0,0,0,date('m'),date('d'),date('Y'))); //今天結束時間 $endToday= date("Y-m-d H:

獲取本週季度本月上月開端日期停止日期 當前日期

第一種方式 Date.prototype.Format = function (fmt) {       var o = {         "M+": this.getMonth() + 1, //月份          "d+": this.getDate(), //日

PHP獲取上週本週上月本月季度季度時間方法大全

<?php echo date("Ymd",strtotime("now")), "\n"; echo date("Ymd",strtotime("-1 week Monday")), "\n"; echo date("Ymd",strtoti

oracle獲取本週本月季度年度的第一天和最後一天

--本週select  trunc(sysdate,'d')+1 from dual;--下週一select  trunc(sysdate,'d')+7from dual; --本月select trunc(sysdate,'mm') from dual;select  last_day(trunc(sy

Java獲取當天本週本月季度本年等 開始及結束時間

package com.zhaochao.utils; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class DataUill { public static

PHP獲取上周上月本月季度季度時間

rto time mon body eas sta weekday 天數 echo echo date("Y-m-d",strtotime("now")); echo "<br>"; echo date("Y-m-d",strtotime("-1 wee

Java獲取當天本月季度本年等 開始及結束時間

tps 結束 sdf gettime util imp public net ast package com.zhaochao.utils; import java.text.SimpleDateFormat; import java.util.Calenda

js 獲取本週上週下週

    <#-- 獲取上週、下週-->      function  getThisWeek(year,month,day)  {          var today=new Date(year,month-1,day);          var week=today.getDay();  

mysql 查詢當天周,本月,上一個月的數據

sql size 表名 BE 上年 時間 unix select 上一個 今天 select * from 表名 where to_days(時間字段名) = to_days(now()); 昨天 SELECT * FROM 表名 WHERE TO_DAYS( NO

mysql 查詢當天周,本月,上一個月的數據---https://www.cnblogs.com/benefitworld/p/5832897.html

今天 from OS alt AS span cnblogs itl val mysql 查詢當天、本周,本月,上一個月的數據 今天 select * from 表名 where to_days(時間字段名) = to_days(now()); 昨天 SELEC

js 獲取input type="file" 選擇的文件大小文件名稱上次修改時間類型等信息

asc com params name serve func files rip key <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server">

js-獲取用戶移動端網絡類型:wifi4g3g2g...

clas gif 播放視頻 pla 時間 lower src user 工作 今天工作時間很寬裕, 忽然想起,自己做過的所有頁面中,有些頁面經常會面臨用戶在網絡狀態很差的時候打開頁面,頁面是掛了的狀態,感覺很LOW~。 所以我決定在今後的頁面中我需要先判斷用戶的網絡狀

Moment.js獲取當前月份所在季度的開始結束日期

需求:獲取當前月份所在第幾季度,並計算出本季度的開始、結束日期。技術:Moment.js說明:在網上找了一下,沒發現直接獲取的辦法,故自己整合了一下。實現:一、獲取當前所在第幾季度(Moment.js官