javascript 计算两个日期的差值

简介: 代码Typescript版/** * TimeSpan just like the class TimpSpan in C# ,represent the time difference * @class TimeSpan */class TimeSpan { constructor(millionseconds: number) { this.

代码

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

目录
相关文章
|
2月前
|
JavaScript 前端开发
JavaScript Date(日期) 对象
JavaScript Date(日期) 对象
53 2
|
1月前
|
JavaScript 前端开发
|
3月前
|
JavaScript 前端开发
js时间戳转日期时间
js时间戳转日期时间
88 20
|
3月前
|
JavaScript
js计算时间差,包括计算,天,时,分,秒
js计算时间差,包括计算,天,时,分,秒
304 16
|
1月前
|
JavaScript 前端开发 搜索推荐
Moment.js、Day.js、Miment,日期时间库怎么选?
【10月更文挑战第29天】如果你需要一个功能强大、插件丰富的日期时间库,并且对性能要求不是特别苛刻,Moment.js是一个不错的选择;如果你追求极致的轻量级和高性能,那么Day.js可能更适合你;而如果你有一些特定的日期时间处理需求,并且希望在性能和功能之间取得平衡,Miment也是可以考虑的。
|
2月前
|
缓存 JavaScript 前端开发
探索Vue.js中的计算属性与侦听器
【10月更文挑战第5天】探索Vue.js中的计算属性与侦听器
30 1
|
3月前
|
JavaScript 前端开发
|
2月前
|
缓存 JavaScript 前端开发
深入理解Vue.js中的计算属性与侦听属性
【10月更文挑战第5天】深入理解Vue.js中的计算属性与侦听属性
36 0
|
2月前
|
缓存 JavaScript 前端开发
探索Vue.js中的计算属性与侦听器:深入理解与实践
【10月更文挑战第5天】探索Vue.js中的计算属性与侦听器:深入理解与实践
27 0
|
5月前
|
JavaScript
js 精确计算(解决js四则运算精度缺失问题)
js 精确计算(解决js四则运算精度缺失问题)
161 0