两分钟之间的差异,分钟,小时javascript [英] Difference between two dates in minute, hours javascript

查看:126
本文介绍了两分钟之间的差异,分钟,小时javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想找到两个日期之间的区别。我试过这段代码,但它给了我错误的价值。我希望获得两个日期之间的总分钟数,因此我将小时数转换为分钟数并增加到分钟数。

I want to find difference between two dates. I have tried this code but it gives me wrong values. I want get total minutes between two dates, so I am converting hours to minutes and adding to minutes.

var hourDiff = timeEnd - timeStart;
var diffHrs = Math.round((hourDiff % 86400000) / 3600000);
var diffMins = Math.round(((hourDiff % 86400000) % 3600000) / 60000);
diffMins = diffMins + (diffHrs * 60);

这里 timeEnd 2007年1月1日星期一11:30:00 GMT + 0530(印度标准时间)

timeStart 2007年1月1日星期一11:00:00 GMT + 0530(印度标准时间)

如果小时差异我得到 1 ,它应该是 0 并且我得到的分钟 30 这是对的。但是小时应该 0 。我在这里做错了吗?

Here if hours difference I am getting 1, it should be 0 and minutes I am getting 30 that is right. But hours should be 0. Am I doing something wrong here?

推荐答案

尝试:

var diffHrs = Math.floor((hourDiff % 86400000) / 3600000);

Math.round 舍入 0.5 小时差异高达 1 。您只想获得小时变量中的完整小时数,是否使用 Math.floor()

Math.round rounded the 0.5 hour difference up to 1. You only want to get the "full" hours in your hours variable, do you remove all the minutes from the variable with the Math.floor()

这篇关于两分钟之间的差异,分钟,小时javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆