代码

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

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

javascript 计算两个日期的差值的更多相关文章

  1. 【HANA系列】SAP HANA SQL计算两个日期的差值

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP HANA SQL计算两个 ...

  2. 【峰回路转】Excel技巧百例 08.计算两个日期的差值

    在Excel中假设高速计算两个日期之间的差? 比如A日期为:2012/3/12   B日期为:2015/7/29  那么这两个日期之间差几年,差几个月.差多少天? 我们使用DateDif 函数来处理. ...

  3. js计算两个日期的差值

    // 获取两个比较值的毫秒数var postman_confirmtime_ms = Date.parse(new Date(data.postman_confirmtime.replace(/-/g ...

  4. php 日期 - 计算2个日期的差值

    /** * 日期-计算2个日期的差值 * @return int */ public function get_difference($date, $new_date) { $date = strto ...

  5. js中计算两个日期之差

    js中计算两个日期之差            var aBgnDate, aEndDate;            var oBgnDate, oEndDate;            var nYl ...

  6. C 语言实例 - 计算两个时间段的差值

    C 语言实例 - 计算两个时间段的差值 C 语言实例 C 语言实例 计算两个时间段的差值. 实例 #include <stdio.h> struct TIME { int seconds; ...

  7. oracle计算两个时间的差值(XX天XX时XX分XX秒)

    在工作中需要计算两个时间的差值,结束时间 - 开始时间,又不想在js里写function,也不想在java里去计算,干脆就在数据库做了一个函数来计算两个时间的差值.格式为XX天XX时XX分XX秒: 上 ...

  8. javaScript 计算两个日期的天数相差

    一:计算两个日期相差的天数 1 <html> <head> <meta http-equiv="Content-Type" content=" ...

  9. Oracle 计算两个时间的差值

    有两个日期数据START_DATE,END_DATE,欲得到这两个日期的时间差(以天,小时,分钟,秒,毫秒):天:ROUND(TO_NUMBER(END_DATE - START_DATE))小时:R ...

随机推荐

  1. find命令--xargs--exec

    find命令 1.1.find命令选项-name 按照文件名查找-perm 按照文件权限来查找-prune 可以使用find命令排除当前文件夹,不查找-user 可以按照文件属主来查找-group 可 ...

  2. Laravel Eloquent get获取空的数据问题

    在用laravel框架来获取数据,若数据不存在时,以为会返回空,其实不是的,其实是一个 collection 值,会返回如下: object(Illuminate\Database\Eloquent\ ...

  3. mysql的 charset、collation、prefix了解

    charset,即字符集. collation,用于指定数据集如何排序,以及字符串的比对规则,即排序规则. prefix,即数据库里表使用的前缀. /************************* ...

  4. Delphi 数据类型的说明

    简单类型包括实数类型(Real) 和有序类型(Ordinal),有序类型又包括整数类型,字符类型,布尔类型,枚举类型和子界类型等. 数据类型                       范围      ...

  5. double和real型有什么区别 [

    DOUBLE是双精度浮点数REAL  是实数类型,他包括 DOUBLE,SINGLE等类型

  6. shell基本理论知识

    (1)查看系统上安装了哪些shell # cat /etc/shells # /etc/shells: valid login shells /bin/sh /bin/dash /bin/bash / ...

  7. Chapter5 – 碰撞检测

    主人公能够放子弹了,虽然子弹看起来很美,但是怎么样来打到妖怪? 在这一章我们介绍一下最简单的碰撞检测方法去实现它. 首先第一个,我们有必要保存每个妖怪和子弹的指针,来够追踪他们的位置. 在这个游戏中我 ...

  8. [Angular Tutorial] 14 -Animations

    在这一步中,我们将会通过在我们先前创建的模板代码中添加CSS和JavaScript动画效果来扩展我们的web应用. ·我们现在使用ngAnimate模块来允许动画效果贯穿整个应用. ·我们也依赖于自带 ...

  9. PreparedStatement接口

    从实际来讲,Statement现在已经不使用了,他已经称为了历史. Statement执行关键性问题在于他需要一个完整 的字符串定义要使用的SQL语句,而PreparedStatement可以动态的设 ...

  10. SpringMVC常用注解@Controller,@Service,@repository,@Component

    SpringMVC常用注解@Controller,@Service,@repository,@Component controller层使用@controller注解 @Controller 用于标记 ...