/**
* 比较两个时间的时间差
* @param startTime 开始时间
* @param endTime 结束时间
* @demo compareTime(new Date('2019-12-24 16:02').getTime(), new Date().getTime())
*/
function compareTime (startTime, endTime) {
var retValue = {} var compareTime = endTime - startTime // 时间差的毫秒数 // 计算出相差天数
var days = Math.floor(compareTime / (24 * 3600 * 1000))
retValue.Days = days // 计算出相差年数
var years = Math.floor(days / 365)
retValue.Years = years // 计算出相差月数
var months = Math.floor(days / 30)
retValue.Months = months // 计算出小时数
var leaveHours = compareTime % (24 * 3600 * 1000) // 计算天数后剩余的毫秒数
var hours = Math.floor(leaveHours / (3600 * 1000))
retValue.Hours = hours // 计算相差分钟数
var leaveMinutes = leaveHours % (3600 * 1000) // 计算小时数后剩余的毫秒数
var minutes = Math.floor(leaveMinutes / (60 * 1000))
retValue.Minutes = minutes // 计算相差秒数
var leaveSeconds = leaveMinutes % (60 * 1000) // 计算分钟数后剩余的毫秒数
var seconds = Math.round(leaveSeconds / 1000)
retValue.Seconds = seconds var resultSeconds = 0
if (years >= 1) {
resultSeconds = resultSeconds + years * 365 * 24 * 60 * 60
}
if (months >= 1) {
resultSeconds = resultSeconds + months * 30 * 24 * 60 * 60
}
if (days >= 1) {
resultSeconds = resultSeconds + days * 24 * 60 * 60
}
if (hours >= 1) {
resultSeconds = resultSeconds + hours * 60 * 60
}
if (minutes >= 1) {
resultSeconds = resultSeconds + minutes * 60
}
if (seconds >= 1) {
resultSeconds = resultSeconds + seconds
}
retValue.resultSeconds = resultSeconds return retValue
}

  

JS比较两个时间的时间差的更多相关文章

  1. C#两个时间的时间差的方法

    今天遇到一问题,计算两个时间的时间差,看网上的写法较为复杂,找到个简单点的,记录下作为自己的总结. 关键函数: DateTime.Subtract 函数解释: 从此实例中减去指定的日期和时间,返回一个 ...

  2. C#计算两个时间的时间差,精确到年月日时分秒

    喏,计算两个时间的时间差,精确到年月日时分秒 看起来比较笨的方法了,不知道有没有改进 DateTime d1 = new DateTime(2016, 4, 1, 0, 0, 0); DateTime ...

  3. js计算两个时间相差天数

     //两个时间相差天数 兼容firefox chrome    function datedifference(sDate1, sDate2) {    //sDate1和sDate2是2006-12 ...

  4. js计算两个时间之间的间隔

    计算时间间隔的方法有很多,这里只是一种方式,可以方法里直接传入两个时间,也可以传入两个字符串格式的时间,在方法里解析一下就ok,这个例子里不传入参数,直接在方法里随便写一个时间来演示一下 fun() ...

  5. JavaScript获取两个时间的时间差

    <html><head><title>JavaScript计算两个时间差</title><meta http-equiv="conten ...

  6. JS计算距当前时间的时间差

    /** * JS获取距当前时间差 * * @param int time JS毫秒时间戳 * */ function get_time_diff(time) { var diff = ''; var ...

  7. iOS-----------计算两个时间的时间差

    UIButton * nameButton = [UIButton buttonWithType:UIButtonTypeCustom]; nameButton.frame = CGRectMake( ...

  8. JS 判断两个时间的大小(可自由选择精确度:天,小时,分钟,秒)

    //可自由选择精确度 如:签到时间:2018-11-07 11:00:00 签退时间:2018-11-07 10:59:59 //判断时间先后 //统一格式 var a = $("#fdtm ...

  9. php如何判断两个时间的时间差

    $time1=2011-11-11 11:11:11$time2=2016-12-10 16:58:13 代码: if(abs(strtotime($time2)-strtotime($time1)) ...

随机推荐

  1. rsync aws ec2 pem

    How to use aws ec2 pem http://www.anthonychambers.co.uk/blog/rsync-to-aws-ec2-using-.pem-key/9 方法如下: ...

  2. List of Mozilla-Based Applications

    List of Mozilla-Based Applications The following is a list of all known active applications that are ...

  3. sso单点登录原理详解

    sso单点登录原理详解     01 单系统登录机制    1.http无状态协议 web应用采用browser/server架构,http作为通信协议.http是无状态协议,浏览器的每一次请求,服务 ...

  4. 十五、RF操作时间控件

    由于日期控件经常用的是readonly属性,这个属性意思是此控件为可读,明白点就是只让你看,不让你动. 解决方法就是:用js去掉这个属性,就可写了,就能输入了 导入库:DateTime #方式一 op ...

  5. 如何使用Jetbrains Clion 在一个工程里 编译单个C++源文件 (实现一键编译且运行)

    这篇文章主要在下面这篇文章的基础上,先是实现了一键编译和一键运行两个单个功能,最后又进一步使用Clion自带的Custom Build Application实现编译且运行一键实现. https:// ...

  6. 【c++进阶:c++ algorithm的常用函数】

    c++ algorithm的常用函数 https://blog.csdn.net/hy971216/article/details/80056933 reverse() reverse(it,it2) ...

  7. windows的VMWare下NAT共享无线方式上网的配置

    1,本文参看: https://blog.51cto.com/13648313/2095288 VMware安装最新版CentOS7图文教程 https://blog.csdn.net/q215879 ...

  8. 八:flask-重定向示例

    现象:访问地址a,跳转到地址b,在flask中,使用redirect()来进行重定向 永久性重定向:301,多用于旧网址被废弃了,需要跳转到新网址访问 例如请求www.jingdong.com,会自动 ...

  9. CentOS安Elasticsearch

    工作中有需求用到es做数据分析和日志搜索的,整理记录一下安装部署过程.ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful we ...

  10. centos7下执行firewall-cmd显示ImportError: No module named 'gi'

    centos7 安装tomcat 及问题处理(No module named 'gi')(Job for firewalld.service failed because the control) 2 ...