zoukankan      html  css  js  c++  java
  • tinydate.js[v0.4] 修复了存在的Bug对于时间diff函数的结果进行了优化

    更新记录

    • 更改了diff函数的计算方式
    • 修复了已知的BUG
    Date.prototype.format = 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), //季度 
            "f+": 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;
    }
    //当前完整时间
    Date.prototype.$nowDate = new Date().format("yyyy-MM-dd HH:mm:ss.ffff");
    //获取自定义格式的当前时间
    Date.prototype.$now = function (fmt) {
        return new Date().format(fmt);
    }
    
    Date.prototype.diff = function (sDate, eDate) {
        if (eDate == undefined || eDate == null)
            eDate = new Date();
        var stime = sDate.getTime();
        var etime = eDate.getTime();
    
        var diffTime = etime - stime;
    
        var timeSpan = new TimeSpan(diffTime);
        return timeSpan;
    }
    
    Date.prototype.addYear = function (number) {
        this.setFullYear(this.getFullYear() + number);
    }
    //添加月
    Date.prototype.addMonth = function (number) {
        this.setMonth(this.getMonth() + number);
    }
    //添加日
    Date.prototype.addDate = function (number) {
        this.setDate(this.getDate() + number);
    }
    //添加小时
    Date.prototype.addHours = function (number) {
        this.setHours(this.getHours() + number);
    }
    //添加分
    Date.prototype.addMinutes = function (number) {
        this.setMinutes(this.getMinutes() + number);
    }
    //添加秒
    Date.prototype.addSeconds = function (number) {
        this.setSeconds(this.getSeconds() + number);
    }
    //添加毫秒
    Date.prototype.addMilliseconds = function (number) {
        this.setMilliseconds(this.getMilliseconds() + number);
    }
    //获得一年中第一天的日期
    Date.prototype.getTheFirstDateOfTheYear = function (date) {
        var year, month=0, day=1;
        if (date == undefined || date == null) {
            year = this.getFullYear();
        }
        else {
            year = date.getFullYear();
        }
        this.setMilliseconds
        return new Date(year, month, day);
    }
    //获得一年中最后一天的日期
    Date.prototype.getTheLastDateOfTheYear = function (date) {
        var year, month = 11, day = 31;
        if (date == undefined || date == null) {
            year = this.getFullYear();
        }
        else {
            year = date.getFullYear();
        }
    
        return new Date(year, month, day);
    }
    Date.prototype.timeSpan = function () {
        return new TimeSpan(this);
    }
    //时限
    function TimeSpan() {
        var o = new Object();
        o.year = 0;
        o.month = 0;
        o.day = 0;
        o.hours = 0;
        o.minutes = 0;
        o.seconds = 0;
        o.milliseconds = 0;
        o.totalYear = 0.00;
        o.totalMonth = 0.00;
        o.totalDay = 0.00;
        o.totalHours = 0.00;
        o.totalMinutes = 0.00;
        o.totalSeconds = 0.00;
        o.totalMilliseconds = 0.00;
    
        o.init = function (timestamp) {
            var odate = new Date(timestamp);
            o.year = odate.getFullYear();
            o.month = odate.getMonth() + 1;
            o.day = odate.getDate();
            o.hours = odate.getHours();
            o.minutes = odate.getMinutes();
            o.seconds = odate.getSeconds();
            o.milliseconds = odate.getMilliseconds();
    
            o.totalMilliseconds = timestamp;
            o.totalSeconds = (timestamp / 1000).toFixed(2);
            o.totalMinutes = (timestamp / 1000 / 60).toFixed(2);
            o.totalHours = (timestamp / 1000 / 60 / 60).toFixed(2);
            o.totalDay = (timestamp / 1000 / 60 / 60 / 24).toFixed(2);
            o.totalMonth = o.year * 12;
            o.totalYear = o.year;
        }
    
        if (arguments.length == 0) {
    
        }
        else if (typeof (arguments[0]) === "string") {
            o.init(new Date(arguments[0]));
        }
        else if (typeof (arguments[0]) === "number") {
            o.init(arguments[0]);
        } else if (typeof(arguments[0]) === "object") {
            o.init(arguments[0]);
        }
        return o;
    }
    //字符串转换为Date
    String.prototype.convertToDate = function () {
        return new Date(Date.parse(this.replace(/-/g, "/")));
    }
    
    //字符串转换为TimeSpan
    String.prototype.convertToTimeSpan = function () {
        return new TimeSpan(new Date(Date.parse(this.replace(/-/g, "/"))));
    }
    
    
    
  • 相关阅读:
    函数初识
    文件操作
    CentOS 7运维管理笔记(6)----Apache 基于 IP 的虚拟主机配置
    CentOS 7运维管理笔记(4)----安装ftp服务器
    CentOS 7运维管理笔记(3)----Linux路由器配置
    CentOS 7运维管理笔记(2)----修改命令提示符颜色
    CentOS 7运维管理笔记(1)----设置默认启动模式为GUI模式或命令行模式
    CentOS 7运维管理笔记(5)----源代码安装Apache 2.4,搭建LAMP服务器
    CentOS 7 下 安装Webmin 启动防火墙失败----Applying firewall rules:iptables-restore:line 2 failed
    CentOS 7 禁用IPV6以提高网速
  • 原文地址:https://www.cnblogs.com/nozer1993/p/9244431.html
Copyright © 2011-2022 走看看