zoukankan      html  css  js  c++  java
  • 日期操作类

    1.Date对象的方法:

      Date()  返回当日的日期和时间
        getDate()   从Date对象返回一个月中的某一天(1-31)
        getDay()    从Date对象返回一周中的某一天(0-6)
        getMonth()  从Date对象返回月份(0-11)
        getFullYear()   从Date对象以四位数字返回年份
        getHours()  返回Date对象的小时(0-23)
        getMinutes()    返回Date对象的分钟(0-59)
        getSeconds()    返回Date对象的秒数(0-59)
        getMilliSecond()    返回Date对象的毫秒数(0-999)
        getTime()   返回从1970年1月1日至今的毫秒数
        setDate()   设置Date对象中的某一天(1-31)
        setMonth()  设置Date对象的月份(0-11)
        setFullYear()   设置Date对象的年份
        setHourse() 设置Date对象的小时(0-23)
        setMinutes()    设置Date对象的分钟(0-59)
        setSeconds()    设置Date对象的秒数(0-59)
        setMilliSeconds()   设置Date对象的毫秒数(0-999)
        setTime()   以毫秒设置Date对象
        // 下面的转换浏览器不同结果不同
        toString()  把Date转成字符串("Mon Dec 19 2016 16:51:18 GMT+0800 (中国标准时间)")
        toTimeString()  把Date的时间部分转成字符串
        toDateString()  把Date的日期部门转成字符串
        toLocaleString()  把Date转成字符串("2016/12/19 下午4:52:01")
        toLocaleTimeString()  把Date的时间部分转成字符串
        toLocaleDateString()  把Date的日期部门转成字符串
        //下面的是自己新加的方法
        dateAdd(strType, Number) Date类型日期增加或减少
        dateFormat(fmt)  格式化日期(依赖jquery)
        toArray()  转化成数组(年月日时分秒)
        isLeapYear()  判断是否闰年
      //Date对象相关函数
    function daysBetween(DateOne, DateTwo) 比较日期相距天数
    function dateCompare(DateOne, DateTwo) 比较日期大小
    function strToDate(str) 转化成Date对象
     

    2.自己新加的方法:

    (1) dateAdd(strType, Number);

    /**
     * description  在Date类型的基础上进行日期增加或减少
     * @param {String} strType 增加的类型(s(秒)、m(分)、h(时)、d(日)、M(月)、y(年)、星期(w)、季度(q))
     * @param {Number} Number  数值(整数为加,负数为减)
     * @example
     *     new Date().dateFormat("yyyy-MM-dd hh:mm:ss")
     *         ==>"2016-12-19 11:07:36"
     *     new Date().DateAdd("h",12).dateFormat("yyyy-MM-dd hh:mm:ss")
     *         ==>"2016-12-19 23:07:39"
     *     new Date().DateAdd("y",12).dateFormat("yyyy-MM-dd hh:mm:ss")
     *         ==>"2028-12-19 23:07:39"
     * @return {Date} 计算结果
     */
    Date.prototype.dateAdd = function(strType, Number) {
        var dtTmp = this;
        switch (strType) {
            case 's':
                /**/
                return new Date(Date.parse(dtTmp) + (1000 * Number));
            case 'm':
                /**/
                return new Date(Date.parse(dtTmp) + (60000 * Number));
            case 'h':
                /**/
                return new Date(Date.parse(dtTmp) + (3600000 * Number));
            case 'd':
                /**/
                return new Date(Date.parse(dtTmp) + (86400000 * Number));
            case 'w':
                /*星期*/
                return new Date(Date.parse(dtTmp) + ((86400000 * 7) * Number));
            case 'q':
                /*季度*/
                return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()) + Number * 3, dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds());
            case 'M':
                /**/
                return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()) + Number, dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds());
            case 'y':
                /**/
                return new Date((dtTmp.getFullYear() + Number), dtTmp.getMonth(), dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds());
        }
    }

    (2) dateFormat(fmt);

    /**
     * @ description 格式化日期类型
     * @ param {String} fmt 定义格式类型(如:yyyy-MM-dd hh:mm:ss)
     * @ param {Date} new Date()  Fri Dec 16 2016 11:05:26 GMT+0800 (中国标准时间)
     * @ example
     * 例:new Date().dateFormat("yyyy-MM-dd")
     *     ==>"2016-12-16"
     * @return {String}  给定格式的String字符串
     */
    Date.prototype.dateFormat = function(fmt) {
        var o = {
            "M+": this.getMonth() + 1, //月份
            "d+": this.getDate(), //
            "h+": this.getHours(), //小时
            "m+": this.getMinutes(), //
            "s+": this.getSeconds(), //
            "q+": Math.floor((this.getMonth() + 3) / 3), //季度
            "S": this.getMilliseconds() //毫秒
        };
        if (/(y+)/.test(fmt)) {
            fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
        }
        for (var k in o) {
            if (new RegExp("(" + k + ")").test(fmt)) {
                fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
            }
        }
        return fmt;
    }

    (3) toArray();

    /**
     * 将标准日期转化为数组年、月、日、时、分、秒
     *  @ example
     * 例:new Date().toArray()
     *    ==>[2016, 12, 16, 15, 10, 59]
     * @return {Array} 包含年、月、日、时、分、秒的数组
     */
    Date.prototype.toArray = function() {
        var myDate = this;
        var myArray = Array();
        myArray[0] = myDate.getFullYear();
        myArray[1] = myDate.getMonth() + 1;
        myArray[2] = myDate.getDate();
        myArray[3] = myDate.getHours();
        myArray[4] = myDate.getMinutes();
        myArray[5] = myDate.getSeconds();
        return myArray;
    }

    (4) isLeapYear();

    /**
     * description 判断Date类型的年份是否是闰年
     * @example
     *  例:new Date().isLeapYear()
     *      ==> true
     * @return {Boolean} 是闰年返回true,反之返回false
     */
    Date.prototype.isLeapYear = function() {
        return (0 == this.getYear() % 4 && ((this.getYear() % 100 != 0) || (this.getYear() % 400 == 0)));
    }

     (5) daysBetween(DateOne, DateTwo);

    /**
     * daysBetween 求两个时间之间的天数差,数据格式:YYYY-MM-dd
     * @param  {String} DateOne  日期1
     * @param  {String} DateTwo  日期2
     * @return {Num}  返回两个日期之间相差的天数
     */
    function daysBetween(DateOne, DateTwo) {
        var OneMonth = DateOne.substring(5, DateOne.lastIndexOf('-'));
        var OneDay = DateOne.substring(DateOne.length, DateOne.lastIndexOf('-') + 1);
        var OneYear = DateOne.substring(0, DateOne.indexOf('-'));
    
        var TwoMonth = DateTwo.substring(5, DateTwo.lastIndexOf('-'));
        var TwoDay = DateTwo.substring(DateTwo.length, DateTwo.lastIndexOf('-') + 1);
        var TwoYear = DateTwo.substring(0, DateTwo.indexOf('-'));
    
        var cha = ((Date.parse(OneMonth + '/' + OneDay + '/' + OneYear) - Date.parse(TwoMonth + '/' + TwoDay + '/' + TwoYear)) / 86400000);
    
        return Math.abs(cha);
    }

     (6) dateCompare(DateOne, DateTwo);

    /**
     * dateCompare 比较两个日期的大小
     * @param  {Date} DateOne 第一个Date对象
     * @param  {Date} DateTwo 第一个Date对象
     * @example
     *   例:dateCompare(new Date(2016,11,12), new Date(2016,11,11));
     *       ==>true
     * @return {Boolean}  如果第一个日期大于第二个日期返回true
     */
    function dateCompare(DateOne, DateTwo) {
        return Date.parse(DateOne) > Date.parse(DateTwo);
    }

     (7) strToDate(str);  

    /**
     * strToDate 将String的字符串转化成Date对象
     * @param  {str} str "yyyy/(-)MM/(-)dd hh:mm:ss或yyyy/(-)MM/(-)dd或yyyy/MM/dd hh:mm:ss"
     * @return {Date}  返回Date类型的时间对象
     */
    function strToDate(str){
        var dateStr = str.replace(/-/g,   "/");
        var str = dateStr.split(" ");
        var arrDate = str[0].split("/");
        arrDate[1]--;
        if(str.length>1){
            var hms = str[1].split(":")
        }else{
            hms = [0,0,0];
        }
        return new Date(arrDate[0], arrDate[1], arrDate[2], hms[0], hms[1], hms[2]);
    }
  • 相关阅读:
    java 在线网络考试系统源码 springboot mybaits vue.js 前后分离跨域
    springboot 整合flowable 项目源码 mybiats vue.js 前后分离 跨域
    flowable Springboot vue.js 前后分离 跨域 有代码生成器 工作流
    Flowable 工作流 Springboot vue.js 前后分离 跨域 有代码生成器
    java 企业 网站源码 后台 springmvc SSM 前台 静态化 代码生成器
    java 进销存 商户管理 系统 管理 库存管理 销售报表springmvc SSM项目
    基于FPGA的电子计算器设计(中)
    基于FPGA的电子计算器设计(上)
    FPGA零基础学习:SPI 协议驱动设计
    Signal tap 逻辑分析仪使用教程
  • 原文地址:https://www.cnblogs.com/daydayupyxq/p/6200949.html
Copyright © 2011-2022 走看看