string 如何在javascript中比较时间?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19004950/
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 to compare time in javascript?
提问by Basel
I have two time in the format "HH:MM" i want to compare them i have the following code to get the time of now in my format:
我有两次格式为“HH:MM”我想比较它们我有以下代码以我的格式获取现在的时间:
current_time = new Date();
hour = current_time.getHours();
minute = current_time.getMinutes();
if(hour<10){hour='0'+hour} if(minute<10){minute='0'+minute}
my_time = hour+':'+minute;
And this code is to get the time after subtracting the GMT difference :
而这段代码是在减去GMT差异后得到时间:
d = new Date()
var n = d.getTimezoneOffset();
var n1 = Math.abs(n);
var difference = (n1/60);
my_time = my_time - (0+difference);
Now the value of my_time should be compared with the value of match_time:
现在应将 my_time 的值与 match_time 的值进行比较:
match_time = 10:00;//for example
if(my_time > match_time)
{
alert('yes');
}
else
{
alert('No');
}
how can I compare those values as time when they are a string ???
当它们是字符串时,我如何将这些值与时间进行比较???
回答by Gabo Esquivel
use Date objects. Date.setHours()allows you to specify hour, minutes, seconds
使用日期对象。Date.setHours()允许你指定小时、分钟、秒
var currentD = new Date();
var startHappyHourD = new Date();
startHappyHourD.setHours(17,30,0); // 5.30 pm
var endHappyHourD = new Date();
endHappyHourD.setHours(18,30,0); // 6.30 pm
console.log("happy hour?")
if(currentD >= startHappyHourD && currentD < endHappyHourD ){
console.log("yes!");
}else{
console.log("no, sorry! between 5.30pm and 6.30pm");
}
回答by Arjun Sol
Date.parse('25/09/2013 13:31') > Date.parse('25/09/2013 9:15')
EDIT:
编辑:
Note that you are parsing an arbitrary date that you're not interested in, it just needs to be the same on both sides.
请注意,您正在解析您不感兴趣的任意日期,它只需要在双方相同。
回答by saidesh kilaru
if(Date.parse('01/01/2011 10:20:45') == Date.parse('01/01/2011 5:10:10')) {
alert('same');
}else{
alert('different');
}
The 1st January is an arbitrary date, doesn't mean anything.
回答by TMurphy
If I had enough reps to vote for @Gabo Esquivel solution. For days I have searched for and tested solutions and this is the only one that worked for me.
如果我有足够的代表投票给 @Gabo Esquivel 解决方案。几天来,我一直在寻找并测试解决方案,这是唯一对我有用的解决方案。
I needed a conditional statement testing if the current time is 0830 and if so, do something. My if statement was not working so I needed other examples to work with.
我需要一个条件语句来测试当前时间是否为 0830,如果是,请执行某些操作。我的 if 语句不起作用,所以我需要使用其他示例。
//Business Hours: Saturday 8:30am-12pm; highlight Saturday table row.
function showSaturdayHours() {
var today = new Date();
var weekday = today.getDay();
var saturdayOpen = new Date();
saturdayOpen.setHours(8, 30, 0);
var saturdayClose = new Date();
saturdayClose.setHours(12, 0, 0);
if (weekday == 6) {
$('#saturday-row').addClass('row-blue'); //highlight table row if current day is Saturday.
if (today >= saturdayOpen && today < saturdayClose) {
document.getElementById('saturday-open').innerHTML = 'Open';
} else {
document.getElementById('saturday-open').innerHTML = 'Closed';
}
}
}
Business Hour Table : JSFiddle
营业时间表:JSFiddle
回答by Ragulan
we can do some hack with.
我们可以做一些黑客。
var time1 = "09:30";
var time2 = "10:30";
var time1Date= new Date("01/01/2000 "+time1);
var time2Date= new Date("01/01/2000 "+time2);
if(time1Date >= time2Date ){
console.log("time1");
}else{
console.log("time2");
}
回答by Pearce
Similar to @Arjun Sol, instead of using Date.parse, you could just grab the times from the string itself, create a new Date object and do the comparison.
与@Arjun Sol 类似,您可以不使用 Date.parse,而是从字符串本身获取时间,创建一个新的 Date 对象并进行比较。
const time1 = '12:42';
const time2 = '18:30';
const getTime = time => new Date(2019, 9, 2, time.substring(0, 2), time.substring(3, 5), 0, 0);
const result = getTime(time1) < getTime(time2);
console.log('This should be true:', result);