jQuery 如何计算Javascript中字符串之间的时间差
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17157337/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I calculate the time difference between strings in Javascript
提问by user987055
is that I have two hours in string format and I need to calculate the difference in javascript, an example:
是我有两个小时的字符串格式,我需要计算javascript中的差异,一个例子:
a = "10:22:57"
a = "10:22:57"
b = "10:30:00"
b = "10:30:00"
difference = 00:07:03 ?
差异 = 00:07:03 ?
回答by Felix Kling
Although using Date
or a library is perfectly fine (and probably easier), here is an example of how to do this "manually" with a little bit of math. The idea is the following:
尽管使用Date
或 库非常好(并且可能更容易),但这里有一个示例,说明如何通过一点数学“手动”执行此操作。想法如下:
- Parse the string, extract hour, minutes and seconds.
- Compute the total number of seconds.
- Subtract both numbers.
- Format the seconds as
hh:mm:ss
.
- 解析字符串,提取小时、分钟和秒。
- 计算总秒数。
- 减去两个数字。
- 将秒格式化为
hh:mm:ss
.
Example:
例子:
function toSeconds(time_str) {
// Extract hours, minutes and seconds
var parts = time_str.split(':');
// compute and return total seconds
return parts[0] * 3600 + // an hour has 3600 seconds
parts[1] * 60 + // a minute has 60 seconds
+parts[2]; // seconds
}
var difference = Math.abs(toSeconds(a) - toSeconds(b));
// compute hours, minutes and seconds
var result = [
// an hour has 3600 seconds so we have to compute how often 3600 fits
// into the total number of seconds
Math.floor(difference / 3600), // HOURS
// similar for minutes, but we have to "remove" the hours first;
// this is easy with the modulus operator
Math.floor((difference % 3600) / 60), // MINUTES
// the remainder is the number of seconds
difference % 60 // SECONDS
];
// formatting (0 padding and concatenation)
result = result.map(function(v) {
return v < 10 ? '0' + v : v;
}).join(':');
回答by Renan
Make two Date
objects out of them. Then you can compare.
Date
用它们制作两个物体。然后你可以比较。
Get the value out of both dates you wish to compare, and make a subtraction. Like this (supposing foo
and bar
are dates):
从要比较的两个日期中获取值,然后进行减法。像这样(假设foo
和bar
是日期):
var totalMilliseconds = foo - bar;
That will give you the amount of milliseconds between both. Some math will convert that to days, hours, minutes, seconds or whatever unit you wish to use. For example:
这将为您提供两者之间的毫秒数。一些数学运算会将其转换为天、小时、分钟、秒或您希望使用的任何单位。例如:
var seconds = totalMilliseconds / 1000;
var hours = totalMilliseconds / (1000 * 3600);
As for obtaining a Date
from a string
, you'll have to look into the constructor (check the first link), and use it in the way that suits you best. Happy coding!
至于Date
从 a获取 a string
,您必须查看构造函数(检查第一个链接),并以最适合您的方式使用它。快乐编码!
回答by dandavis
a really easy way if you will always have fewer than 12 hours:
如果您的时间总是少于 12 小时,这是一个非常简单的方法:
a = "10:22:57";
b = "10:30:00";
p = "1/1/1970 ";
difference = new Date(new Date(p+b) - new Date(p+a)).toUTCString().split(" ")[4];
alert( difference ); // shows: 00:07:03
if you need to format for more than 12 hours, it's more complicated to render, the # of MS between the dates is correct using the this math...
如果您需要格式化超过 12 小时,则渲染起来会更复杂,使用此数学计算,日期之间的 MS 数是正确的...
回答by flow
You have to use Date objects : http://www.w3schools.com/jsref/jsref_obj_date.asp
您必须使用日期对象:http: //www.w3schools.com/jsref/jsref_obj_date.asp
Then compare : How to calculate date difference in javascript
然后比较: 如何计算javascript中的日期差异