[英]How to handle hours difference between times
I have jquery hours range picker and I am not using 'am' & 'pm' notation. 我有jquery小时范围选择器,并且没有使用'am'和'pm'表示法。 I prefer 24h.
我更喜欢24小时。 I need to calculate the hours difference between selected hours.
我需要计算所选小时之间的小时差。
User selects '09:00' & '12:00' -> 3 hours
用户选择“
'09:00' & '12:00' -> 3 hours
My problem is with if he selects '20:00' & '00:00' -> must be 4
hours but gives negative. 我的问题是,如果他选择“
'20:00' & '00:00' -> must be 4
小时,但给出的是负数。 Alternatively; 另外;
'23:00' & '02:00' -> negative
Should I take abs? 我应该吸收腹肌吗? or any other way?
或其他方式?
You need to use modulo. 您需要使用模数。 Unfortunately Javascript's modulo is wrong.
不幸的是Javascript的模是错误的。
Number.prototype.mod = function(n) { return ((this % n) + n) % n; }; console.log((0 - 20).mod(24));
Simply add if check to the difference you get and if it's negative add 24 hours to it 只需添加是否检查得到的差额,如果为负,则添加24小时
var diff = function(a,b) {
var difference = b - a;
if (difference < 0) {
return difference + 24;
} else {
return difference;
}
}
Of course you will need to parse your time first. 当然,您将需要首先解析您的时间。 Here is a fiddle with alerts instead of returns.
这是一个警告而不是回报的小提琴。 But this is the easiest way i guess, but mb not the best one fiddle
但这是我猜出的最简单的方法,但不是最好的小提琴
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.