JS比较两个时间的时间差
/**
* 比较两个时间的时间差
* @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比较两个时间的时间差的更多相关文章
- C#两个时间的时间差的方法
今天遇到一问题,计算两个时间的时间差,看网上的写法较为复杂,找到个简单点的,记录下作为自己的总结. 关键函数: DateTime.Subtract 函数解释: 从此实例中减去指定的日期和时间,返回一个 ...
- C#计算两个时间的时间差,精确到年月日时分秒
喏,计算两个时间的时间差,精确到年月日时分秒 看起来比较笨的方法了,不知道有没有改进 DateTime d1 = new DateTime(2016, 4, 1, 0, 0, 0); DateTime ...
- js计算两个时间相差天数
//两个时间相差天数 兼容firefox chrome function datedifference(sDate1, sDate2) { //sDate1和sDate2是2006-12 ...
- js计算两个时间之间的间隔
计算时间间隔的方法有很多,这里只是一种方式,可以方法里直接传入两个时间,也可以传入两个字符串格式的时间,在方法里解析一下就ok,这个例子里不传入参数,直接在方法里随便写一个时间来演示一下 fun() ...
- JavaScript获取两个时间的时间差
<html><head><title>JavaScript计算两个时间差</title><meta http-equiv="conten ...
- JS计算距当前时间的时间差
/** * JS获取距当前时间差 * * @param int time JS毫秒时间戳 * */ function get_time_diff(time) { var diff = ''; var ...
- iOS-----------计算两个时间的时间差
UIButton * nameButton = [UIButton buttonWithType:UIButtonTypeCustom]; nameButton.frame = CGRectMake( ...
- JS 判断两个时间的大小(可自由选择精确度:天,小时,分钟,秒)
//可自由选择精确度 如:签到时间:2018-11-07 11:00:00 签退时间:2018-11-07 10:59:59 //判断时间先后 //统一格式 var a = $("#fdtm ...
- php如何判断两个时间的时间差
$time1=2011-11-11 11:11:11$time2=2016-12-10 16:58:13 代码: if(abs(strtotime($time2)-strtotime($time1)) ...
随机推荐
- (转)GitBlit安装
转:https://blog.csdn.net/qq_32599479/article/details/90748371 GitBlit的安装本文是基于Windows 10系统环境,安装和测试GitB ...
- tensorflow源码分析——LSTMCell
LSTMCell 是最简单的LSTMCell,源码位于:/tensorflow/contrib/rnn/python/ops/core_rnn_cell_impl.py.LSTMCell 继承了RNN ...
- ros 下常用的依赖库
<buildtool_depend>catkin</buildtool_depend> <build_depend>nav_msgs</build_depen ...
- Linux编译部署vsftp服务
部署vsftp服务 Hostname OS IP Software Vsftpsvr RHEL-5.8 10.0.0.58 yum install -y vsftpd yum install –y ...
- 【MM系列】SAP MM模块-控制采购订单中某些项目的输出显示
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP MM模块-控制采购订单中某些 ...
- LeetCode.868-二进制距离(Binary Gap)
这是悦乐书的第333次更新,第357篇原创 01看题和准备 今天介绍的是LeetCode算法题中Easy级别的第203题(顺位题号是868).给定正整数N,找到并返回N的二进制表示中两个连续1之间的最 ...
- Linux 后台执行python或者java代码的命令
1.nohup 命令操作后台执行程序 后台启动 nohup python app.py params1 > nohup.out >& & 查看后台进程启动 jobs -l ...
- 20191127 Spring Boot官方文档学习(6-8)
6.部署Spring Boot应用程序 在部署应用程序时,Spring Boot的灵活打包选项提供了很多选择.您可以将Spring Boot应用程序部署到各种云平台,容器映像(例如Docker)或虚拟 ...
- 前端 CSS 盒子模型 目录
CSS盒子模型介绍 padding border属性
- mybatis使用map传递多参数报错:A query was run and no Result Maps were found for the Mapped Statement
在使用mybatis进行多参数传递时,报错: A query was run and no Result Maps were found for the Mapped Statement 'xx.xx ...