javascript 计算两个日期的差值
代码
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 计算两个日期的差值的更多相关文章
- 【HANA系列】SAP HANA SQL计算两个日期的差值
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP HANA SQL计算两个 ...
- 【峰回路转】Excel技巧百例 08.计算两个日期的差值
在Excel中假设高速计算两个日期之间的差? 比如A日期为:2012/3/12 B日期为:2015/7/29 那么这两个日期之间差几年,差几个月.差多少天? 我们使用DateDif 函数来处理. ...
- js计算两个日期的差值
// 获取两个比较值的毫秒数var postman_confirmtime_ms = Date.parse(new Date(data.postman_confirmtime.replace(/-/g ...
- php 日期 - 计算2个日期的差值
/** * 日期-计算2个日期的差值 * @return int */ public function get_difference($date, $new_date) { $date = strto ...
- js中计算两个日期之差
js中计算两个日期之差 var aBgnDate, aEndDate; var oBgnDate, oEndDate; var nYl ...
- C 语言实例 - 计算两个时间段的差值
C 语言实例 - 计算两个时间段的差值 C 语言实例 C 语言实例 计算两个时间段的差值. 实例 #include <stdio.h> struct TIME { int seconds; ...
- oracle计算两个时间的差值(XX天XX时XX分XX秒)
在工作中需要计算两个时间的差值,结束时间 - 开始时间,又不想在js里写function,也不想在java里去计算,干脆就在数据库做了一个函数来计算两个时间的差值.格式为XX天XX时XX分XX秒: 上 ...
- javaScript 计算两个日期的天数相差
一:计算两个日期相差的天数 1 <html> <head> <meta http-equiv="Content-Type" content=" ...
- Oracle 计算两个时间的差值
有两个日期数据START_DATE,END_DATE,欲得到这两个日期的时间差(以天,小时,分钟,秒,毫秒):天:ROUND(TO_NUMBER(END_DATE - START_DATE))小时:R ...
随机推荐
- [iOS]C语言技术视频-09-枚举的定义
下载地址: 链接: http://pan.baidu.com/s/1o625Ee2 密码: 8kp5
- Apache和PHP的优化
调优 Apache Apache 是一种高度可配置的软件.它具有大量特性,但每一种都代价高昂.从某种程度上来说,调优 Apache 来说就是以恰当的方式分配资源,还涉及到将配置简化为仅包含必要内容. ...
- android测试之——Instrumentation(一)
以下是本人原创,如若转载和使用请注明转载地址.本博客信息切勿用于商业,可以个人使用,若喜欢我的博客,请关注我,谢谢!博客地址 感谢您支持我的博客,我的动力是您的支持和关注!如若转载和使用请注明转载地址 ...
- el5,6,7的ntpdate服务
在el5里没有ntpdate服务 在el6里有ntpdate服务 在el7里有ntpdate服务
- vuejs 父组件向子组件传递($broadcast()的用法)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 文件查找和比较命令 来自: http://man.linuxde.net/find
文件查找和比较1.find命令,用来在指定目录下查找文件.任何位于参数之前的字符串都将被视为欲查找的目录名.如果使用该命令时不设置任何参数,则find命令则在当前目录下查找子目录与文件.并且将查到的子 ...
- Python使用Selenium/PhantomJS
安装selenium: 1 pip install selenium 安装PhantomJS: 1 2 3 4 https://bitbucket.org/ariya/phantomjs/downlo ...
- PHP之Mysql常用SQL语句示例的深入分析
1.插入数据insert into表名(列名1,列名2,列名..) values(值1,值2,值...); insert into product(name, price, pic_path) val ...
- 分布式cookie-session的实现(spring-session)
分布式cookie-session的实现(spring-session) 本文使用的spring-session版本为 1.0.0,地址为: https://github.com/spring-pro ...
- 2015年15+最佳的响应式HTML5网站模板
015年最好的免费响应式HTML5模板通常用于创建新潮的网站. HTML5是HTML用于创建现代化网站的最新版本.随着这一现代标记语言的出现,网上冲浪的趋势变得越来越智能化越来越酷.几乎每个web开发 ...