代码

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. astah* professional 6.9.0

    下载地址:http://members.change-vision.com/files/astah_professional/6_9_0 破解方法:按照Astah Professional 6.9后打 ...

  2. JS表单原生验证器

    一.前言 最近在开发一个新项目,需要做登陆等一系列的表单提交页面.在经过“缜密”的讨论后,我们决定 不用外部流行的框架,如bootstrap,由于我负责的模块 仅仅是其中的一部分,因此少数服从多数,无 ...

  3. MySQL的char和varchar

    一.VARCHAR与CHAR字符型数据的差异 在MySQL数据库中,用的最多的字符型数据类型就是Varchar和Char,这两种数据类型虽然都是用来存放字符型数据,但是无论从结构还是从数据的保存方式来 ...

  4. [bzoj1195] [hnoi2006] 最短母串

    本题是一个经典的状压dp问题,在紫书中有着加强版的例题. 本题的难度主要体现在:如何输出字符串字典序最小. 为了解决这个问题,我们有两种常用方案: 1) 我们可以采用bfs输出路径的方法,使用+1来输 ...

  5. ecshop--标签数组

    $properties  商品属性 array(1) { ["商品属性"]=> array(1) { [178]=> array(2) { ["name&qu ...

  6. Apache Bench安装与使用

    一.Apache Bench简介 ApacheBench 是 Apache 服务器自带的一个web压力测试工具,简称ab.ab又是一个命令行工具,对发起负载的本机要求很低,根据ab命令可以创建很多的并 ...

  7. 【转】图片缓存之内存缓存技术LruCache、软引用 比较

    每当碰到一些大图片的时候,我们如果不对图片进行处理就会报OOM异常,这个问题曾经让我觉得很烦恼,后来终于得到了解决,那么现在就让我和大家一起分享一下吧.这篇博文要讲的图片缓存机制,我接触到的有两钟,一 ...

  8. 【动态规划】Gym - 101102A - Coins

    Hasan and Bahosain want to buy a new video game, they want to share the expenses. Hasan has a set of ...

  9. [Angular Tutorial] 6-Two-way Data Binding

    在这一步中,您将会添加一个新特性来使得您的用户可以控制电话列表中电话的顺序,动态改变顺序是由创建一个新的数据模型的特性实现的,将它和迭代器绑定在一起,并且让数据绑定神奇地处理下面的工作. ·除了搜索框 ...

  10. DP——由蒟蒻到神犇的进阶之路

    开始更新咯 DP专题[题目来源BZOJ] 一.树形DP 1.bzoj2286消耗战 题解:因为是树形结构,一个点与根节点不联通,删一条边即可, 于是我们就可以简化这棵树,把有用的信息建立一颗虚树,然后 ...