代码
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; } }
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; };
原理
1.将两个日期转换成UTC标准时间
2.作差
3.将剩余的差值毫秒计算成天小时什么的
4.把结果放在一个类里
欢迎访问我的GitHub