代码

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. js 如何动态添加数组_百度知道

    1.数组的创建var arrayObj = new Array(); //创建一个数组var arrayObj = new Array([size]); //创建一个数组并指定长度,注意不是上限,是长 ...

  2. Vmware Tools is currently being installed on your system(转)

    Follow the 3 Steps : Restore the /etc/issue file: sudo mv /etc/issue.backup /etc/issue* PS:在本人的PC上执行 ...

  3. xcode磁盘大清理

     转---Xcode磁盘空间大清理 我的设备是Macbook Air 13’ Mid 2011,128G SSD.最近开始有些存储压力了,用Clean My Mac清理一部分旧文件后,决定对Xcode ...

  4. Document Classification

    Natural Language Processing with Python Chapter 6.1 由于nltk.FreqDist的排序问题,获取电影文本特征词的代码有些微改动. import n ...

  5. (简单) POJ 1502 MPI Maelstrom,Dijkstra。

    Description BIT has recently taken delivery of their new supercomputer, a 32 processor Apollo Odysse ...

  6. laravel无法显示路由界面

    安装完laravel项目后,开启了重写,/app/storage也设置好了权限,但是始终无法显示出页面,并出现: "Whoops, looks like something went wro ...

  7. RabbitMQ java 参数

    channel.exchangeDeclare(exchange, "direct", true, false, null); 第一个参数:交换组名字, 第二个参数:队交换组类型: ...

  8. Servlet中的过滤器Filter用法

    1.过滤器的概念 Java中的Filter 并不是一个标准的Servlet ,它不能处理用户请求,也不能对客户端生成响应. 主要用于对HttpServletRequest 进行预处理,也可以对Http ...

  9. Tsinsen-A1489 抽奖 【数学期望】

    乔明达太神,其实已经题解非常清楚了,我再推一遍吧. 题目意思相当于有n个盒子,无差别投m次球,每个盒子的得分为每个盒子里的球的个数. 第一问: 假设这个球放在了第i个盒子里,那么 ∆ans = (mi ...

  10. Memcached源码分析之thread.c

    /* * 文件开头先啰嗦几句: * * thread.c文件代表的是线程模块.但是你会看到这个模块里面有很多其它方法, 例如关于item的各种操作函数,item_alloc,item_remove,i ...