代码

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. java 并发多线程异步

    http://www.cnblogs.com/dolphin0520/category/602384.html   并发 http://blog.csdn.net/column/details/con ...

  2. css 弹出框

    最近想弄一个类似登陆框的那种弹出框,其实网上已经有很多例子,而且也有相应的插件,例如:jquery-ui的,可直接使用,而我就简单的弄了个简易版的登陆框,真的很简易. 其实原理就是设置两个div,一个 ...

  3. 在IOS应用中从竖屏模式强制转换为横屏模式

    http://www.cnblogs.com/mrhgw/archive/2012/07/18/2597218.html 在 iPhone 应用里,有时我们想强行把显示模式从纵屏改为横屏(反之亦然), ...

  4. iOS开发之监听键盘高度的变化

    最近做的项目中,有一个类似微博中的评论转发功能,屏幕底端有一个输入框用textView来做,当textView成为第一响应者的时候它的Y值随着键盘高度的改变而改变,保证textView紧贴着键盘,但又 ...

  5. STM32驱动DS18B20

    DS18B20 是由 DALLAS 半导体公司推出的一种的“一线总线”接口的温度传感器.与传 统的热敏电阻等测温元件相比,它是一种新型的体积小.适用电压宽.与微处理器接口简单的 数字化温度传感器.一线 ...

  6. javascript-函数声明和函数表达式-call-apply

    1.函数声明与函数表达式 <script type="text/javascript"> //函数表达式,解析器在像执行环境中加载数据时,函数表达式是解析器执行到这段代 ...

  7. bzoj1562[NOI2009]变换序列——2016——3——12

    任意门:http://www.lydsy.com/JudgeOnline/problem.php?id=1562 题目: 对于0,1,…,N-1的N个整数,给定一个距离序列D0,D1,…,DN-1,定 ...

  8. 阿里CEO张勇公开信:把眼光从股市回到客户身上

     8月25日消息,面对全球资本市场的剧烈波动,阿里巴巴集团CEO张勇今日发表致员工信,倡议全体阿里员工把眼光从股市回到客户身上,脚踏实地的服务帮助客户,为客户创造价值,继而为股东和自己创造价值.  张 ...

  9. Zepto.js-事件处理

    http://www.webdevs.cn/article/68.html     web开发网 事件 $.Event $.Event(type, [properties]) ⇒ event 创建并初 ...

  10. NodeMCU之旅(三):响应配置按钮

    引言 在之前的代码中,要连接的WIFI信息都已写死在代码里,这显然不能适应我们的需求.所以需要想个办法让用户可以配置这些信息. WIFI工作模式 NodeMCU支持STATION,SOFTAP,STA ...