javascript 比较两个时间 (hh:mm:ss) 字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6632808/
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
Compare two time (hh:mm:ss) strings
提问by Null Pointer
I have two time strings in hh:mm:ss
format. (eg: 12:40:13
and 20:01:01
.) How can I compare these in JavaScript?
我有两个hh:mm:ss
格式的时间字符串。(例如:12:40:13
和20:01:01
。)我如何在 JavaScript 中比较这些?
回答by mplungjan
I prefer to have date objects, but as pointed out elsewhere, you can just convert to seconds if you want to do simple compares
我更喜欢日期对象,但正如其他地方所指出的,如果你想做简单的比较,你可以转换为秒
function dateCompare(time1,time2) {
var t1 = new Date();
var parts = time1.split(":");
t1.setHours(parts[0],parts[1],parts[2],0);
var t2 = new Date();
parts = time2.split(":");
t2.setHours(parts[0],parts[1],parts[2],0);
// returns 1 if greater, -1 if less and 0 if the same
if (t1.getTime()>t2.getTime()) return 1;
if (t1.getTime()<t2.getTime()) return -1;
return 0;
}
alert(dateCompare("12:40:13","20:01:01"));
For seconds:
几秒钟:
function dateDiff(time1,time2) {
var t1 = new Date();
var parts = time1.split(":");
t1.setHours(parts[0],parts[1],parts[2],0);
var t2 = new Date();
parts = time2.split(":");
t2.setHours(parts[0],parts[1],parts[2],0);
return parseInt(Math.abs(t1.getTime()-t2.getTime())/1000);
}
Assuming you have 24 hour times and same padding you can do simple string compare
假设您有 24 小时的时间和相同的填充,您可以进行简单的字符串比较
var t1 = "12:40:13", t2= "20:01:01";
if (t1<t2) {
console.log(t1," is < ", t2);
}
回答by RobG
If "compare" means "see if they are equal", and the two have the same format, why not simply:
如果“比较”的意思是“看它们是否相等”,并且两者具有相同的格式,为什么不简单地:
var time1 = "12:40:13";
var time2 = "20:01:01";
if (time1 == time2) {
// do stuff
}
If you need to get the difference in time, the conversion to a date object is one way (see mplungjan's answer) or you can convert them to a common unit (say seconds) and subtract:
如果您需要获得时间差异,则转换为日期对象是一种方式(请参阅 mplungjan 的答案),或者您可以将它们转换为通用单位(例如秒)并减去:
function toSeconds(t) {
var bits = t.split(':');
return bits[0]*3600 + bits[1]*60 + bits[2]*1;
}
var secs1 = toSeconds(time1);
var secs2 = toSeconds(time2);
// do stuff - compare, subtract, less than, greater than, whatever
回答by twip
Here's another take:
这是另一个观点:
function compareTimes(timeOne, timeTwo) {
if(daterize(timeOne) > daterize(timeTwo)) return 1;
if(daterize(timeOne) < daterize(timeTwo)) return -1;
return 0;
}
function daterize(time) {
return Date.parse("Thu, 01 Jan 1970 " + time + " GMT");
}
You may also want to take a look at the MDN Javascript docs for Dates. Javascript comes with a lot of gotchas, like Date.month going from 0-11.
您可能还想查看Dates的MDN Javascript 文档。Javascript 有很多问题,比如 Date.month 从 0 到 11。
回答by saidesh kilaru
Date.parse('01/01/2011 10:20:45') > Date.parse('01/01/2011 5:10:10')
The 1st January is an arbitrary date, doesn't mean anything.
回答by Benny Tjia
Here is one suggestion that I modified from the solution in this website here, hope it helps.
这是我从本网站此处的解决方案中修改的一项建议,希望对您有所帮助。
function compareTime(time_1, time_2) {
var s1 =
time1.split(":")[0] * 3600 + time1.split(":")[1] * 60 + time1.split(":")[2];
var s2 =
time2.split(":")[0] * 3600 + time2.split(":")[1] * 60 + time1.split(":")[2];
return Math.abs(s1 - s2); // Gets difference in seconds
}
var time_1 = "12:40:13", time_2 = "20:01:01";
document.write(compareTime(time_1, time_2));