zoukankan      html  css  js  c++  java
  • js处理时间

    TOC

    js处理时间

    /**
    * 获取本周、本季度、本月、上月的开始日期、结束日期
    */
    //当前日期
    var now = new Date();
    //今天本周的第几天
    var nowDayOfWeek = now.getDay();
    //当前日
    var nowDay = now.getDate();
    //当前月
    var nowMonth = now.getMonth()+1;
    //当前年 yyyy
    var year=now.getFullYear();
    //当前年 yy
    var nowYear = now.getYear();
    //手动+2000
    nowYear += (nowYear < 2000) ? 1900 : 0;
    //获取当前星期X(0-6,0代表星期天)
    now.getDay();
     //获取当前时间(从1970.1.1开始的毫秒数)
    now.getTime();
    //获取当前小时数(0-23)
    now.getHours();
    //获取当前分钟数(0-59)
    now.getMinutes();
    //获取当前秒数(0-59)
    now.getSeconds();
    //获取当前毫秒数(0-999)
    now.getMilliseconds();
    //获取当前日期
    now.toLocaleDateString();
    //获取当前时间
    now.toLocaleTimeString();
    //获取日期与时间
    now.toLocaleString( );
    
    //获取上月日期1号
    var lastMonthDate = new Date();
    lastMonthDate.setDate(1);
    lastMonthDate.setMonth(lastMonthDate.getMonth() - 1);
    //上月年份
    var lastYear = lastMonthDate.getYear();
    //上月月份
    var lastMonth = lastMonthDate.getMonth()+1;
    
    /*格式化日期: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(year,month) {
        return new Date(year,month-1, 0).getDate();
    }
    
    /*获得本季度的开始月份*/
    function getQuarterStartMonth() {
        var quarterStartMonth = 0;
        var nowMonth = new Date().getMonth()+1;
        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() {
        //当前日期
        let now = new Date();
        //今天本周的第几天
        let nowDayOfWeek = now.getDay();
        //当前日
        let nowDay = now.getDate();
        //当前月
        let nowMonth = now.getMonth();
        //当前年 yyyy
        let nowYear=now.getFullYear();
        let weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek);
        return formatDate(weekStartDate);
    }
    
    /*获得本周的结束日期*/
    function getWeekEndDate() {
        //当前日期
        let now = new Date();
        //今天本周的第几天
        let nowDayOfWeek = now.getDay();
        //当前日
        let nowDay = now.getDate();
        //当前月
        let nowMonth = now.getMonth();
        //当前年 yyyy
        let nowYear=now.getFullYear();
        let weekEndDate = new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek));
        return formatDate(weekEndDate);
    
    }
    
    /*获得本月的开始日期*/
    function getMonthStartDate() {
        //当前日期
        let now = new Date();
        //当前月
        let nowMonth = now.getMonth();
        //当前年 yyyy
        let nowYear=now.getFullYear();
        let monthStartDate = new Date(nowYear, nowMonth, 1);
        return formatDate(monthStartDate);
    
    }
    
    //获得本月的结束日期
    function getMonthEndDate() {
        //当前日期
        let now = new Date();
        //当前月
        let nowMonth = now.getMonth();
        //当前年 yyyy
        let nowYear=now.getFullYear();
        let monthEndDate = new Date(nowYear, nowMonth, getMonthDays(nowYear,nowMonth+1));
        return formatDate(monthEndDate);
    
    }
    
    //获得上月开始时间
    function getLastMonthStartDate() {
        //当前日期
        let now = new Date();
    
        //当前年 yyyy
        let nowYear=now.getFullYear();
        //获取上月日期1号
        let lastMonthDate = new Date();
        lastMonthDate.setDate(1);
        lastMonthDate.setMonth(lastMonthDate.getMonth() - 1);
        //上月月份
        let lastMonth = lastMonthDate.getMonth();
        let lastMonthStartDate = new Date(nowYear, lastMonth, 1);
        return formatDate(lastMonthStartDate);
    
    }
    
    /*获得上月结束时间*/
    function getLastMonthEndDate() {
    //当前日期
        let now = new Date();
    
        //当前年 yyyy
        let nowYear=now.getFullYear();
        //获取上月日期1号
        let lastMonthDate = new Date();
        lastMonthDate.setDate(1);
        lastMonthDate.setMonth(lastMonthDate.getMonth() - 1);
        //上月月份
        let lastMonth = lastMonthDate.getMonth();
        let lastMonthEndDate = new Date(nowYear, lastMonth, getMonthDays(lastMonth+1));
        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);
    }
    




  • 相关阅读:
    undefined reference to cv::imread(cv::String const&, int)
    ubuntu gcc 降级 适应matlab
    ubuntu 迅雷 XwareDesktop
    python 包 安装 加速 pip anaconda
    ubuntu classicmenu-indicator
    ubuntu 电源管理
    apue.h头文件(UNIX环境高级编程)
    ubuntu 12.04 下nginx安装步骤
    Ubuntu12.04 64bit 下安装VNC server
    TLD视觉跟踪算法
  • 原文地址:https://www.cnblogs.com/ziyue7575/p/12191502.html
Copyright © 2011-2022 走看看