zoukankan      html  css  js  c++  java
  • javascript 计算两个日期的差值

    代码

    Typescript版

    /**
     * TimeSpan just like the class TimpSpan in C# ,represent the time difference
     * @class TimeSpan
     */
    class TimeSpan {
        constructor(millionseconds: number) {
            this.totalMillionseconds = millionseconds;
            this.totalSeconds = millionseconds / 1000;
            this.totalMinutes = this.totalSeconds / 60;
            this.totalHours = this.totalMinutes / 60;
            this.totalDays = this.totalHours / 24;
    
            this.day = Math.floor(millionseconds / (1000 * 60 * 60 * 24));
            let surplus = millionseconds % (1000 * 60 * 60 * 24);
            this.hour = surplus / (1000 * 60 * 60);
            surplus = surplus % (1000 * 60 * 60);
            this.minute = surplus / (1000 * 60);
            surplus = surplus % (1000 * 60);
            this.second = surplus / (1000);
            surplus = surplus % (1000);
            this.millionsecond = surplus;
    
        }
    
        totalDays: number;
        totalHours: number;
        totalMinutes: number;
        totalSeconds: number;
        totalMillionseconds: number;
    
    
        day: number;
        hour: number;
        minute: number;
        second: number;
        millionsecond: number;
    
        /**
         * if the date2 later than date 1 ,it's true
         * @type {boolean}
         * @memberOf TimeSpan
         */
        isPositive: boolean;
    }
    
    
    /**
     * The Aqua class
     * @class Aqua
     */
    class Aqua {
    
        /**
         * 将Date对象转换为 UTC 时间 毫秒数
         * convert Date object to UTC time millionseconds
         * @param {Date} date
         * @returns {number}
         */
        UTC(date: Date): number {
            return Date.UTC(
                date.getUTCFullYear(),
                date.getUTCMonth(),
                date.getUTCDate(),
                date.getUTCHours(),
                date.getUTCMinutes(),
                date.getUTCSeconds()
            )
        }
    
        /**
         * compare to two date ,caculate the difference 
         * 对比两个日期,计算他们的差值
         * @param {Date} date1
         * @param {Date} date2
         * @returns {TimeSpan}
         */
        compareDate(date1: Date, date2: Date): TimeSpan {
            let number1 = this.UTC(date1);
            let number2 = this.UTC(date2);
            let isPositive = number2 > number1;
            number1 = Math.abs(number1);
            number2 = Math.abs(number2);
            let res = new TimeSpan(Math.abs(number2 - number1));
            res.isPositive = isPositive;
            return res;
        }
    }
    View Code

    JavaScript版

    /**
     * TimeSpan just like the class TimpSpan in C# ,represent the time difference
     * @class TimeSpan
     */
    var TimeSpan = (function () {
        function TimeSpan(millionseconds) {
            this.totalMillionseconds = millionseconds;
            this.totalSeconds = millionseconds / 1000;
            this.totalMinutes = this.totalSeconds / 60;
            this.totalHours = this.totalMinutes / 60;
            this.totalDays = this.totalHours / 24;
            this.day = Math.floor(millionseconds / (1000 * 60 * 60 * 24));
            var surplus = millionseconds % (1000 * 60 * 60 * 24);
            this.hour = surplus / (1000 * 60 * 60);
            surplus = surplus % (1000 * 60 * 60);
            this.minute = surplus / (1000 * 60);
            surplus = surplus % (1000 * 60);
            this.second = surplus / (1000);
            surplus = surplus % (1000);
            this.millionsecond = surplus;
        }
        return TimeSpan;
    }());
    
       /**
         * 将Date对象转换为 UTC 时间 毫秒数
         * convert Date object to UTC time millionseconds
         * @param {Date} date
         * @returns {number}
         */
        Aqua.prototype.UTC = function (date) {
            return Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(), date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds());
        };
    
    
    
        /**
         * compare to two date ,caculate the difference
         * 对比两个日期,计算他们的差值
         * @param {Date} date1
         * @param {Date} date2
         * @returns {TimeSpan}
         */
        Aqua.prototype.compareDate = function (date1, date2) {
            var number1 = this.UTC(date1);
            var number2 = this.UTC(date2);
            var isPositive = number2 > number1;
            number1 = Math.abs(number1);
            number2 = Math.abs(number2);
            var res = new TimeSpan(Math.abs(number2 - number1));
            res.isPositive = isPositive;
            return res;
        };
    View Code

    原理

    1.将两个日期转换成UTC标准时间

    2.作差

    3.将剩余的差值毫秒计算成天小时什么的

    4.把结果放在一个类里

    欢迎访问我的GitHub

    https://github.com/rocketRobin/aqua-toolbox

  • 相关阅读:
    Java多线程知识-Callable和Future
    C#程序集Assembly学习随笔(增补版,附图)_AX
    C#程序集Assembly学习随笔(第一版)_AX
    【.Net】 C#访问修饰符
    访问修饰符(C# 编程指南)
    Restful API 架构与设计参考原则
    RESTful API 设计指南
    WebService的两种方式SOAP和REST比较
    公众号
    es6(const、let)
  • 原文地址:https://www.cnblogs.com/rocketRobin/p/6370864.html
Copyright © 2011-2022 走看看