检查当前时间是否在 JavaScript 中的两个给定时间之间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29785294/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 11:03:44  来源:igfitidea点击:

Check if current time is between two given times in JavaScript

javascriptdate

提问by Sunil

I have two variables called 'startTime' and 'endTime'. I need to know whether current time falls between startTime and EndTime. How would I do this using JavaScript only?

我有两个变量,名为“startTime”和“endTime”。我需要知道当前时间是否介于 startTime 和 EndTime 之间。我将如何仅使用 JavaScript 来做到这一点?

var startTime = '15:10:10';
var endTime = '22:30:00';
var currentDateTime = new Date(); 
//is current Time between startTime and endTime ???

UPDATE 1:

更新 1

I was able to get this using following code. You can check out the code at: https://jsfiddle.net/sun21170/d3sdxwpb/1/

我能够使用以下代码得到这个。您可以在以下位置查看代码:https: //jsfiddle.net/sun21170/d3sdxwpb/1/

var dt = new Date();//current Date that gives us current Time also

var startTime = '03:30:20';
var endTime = '23:50:10';

var s =  startTime.split(':');
var dt1 = new Date(dt.getFullYear(), dt.getMonth(), dt.getDate(),
                   parseInt(s[0]), parseInt(s[1]), parseInt(s[2]));

var e =  endTime.split(':');
var dt2 = new Date(dt.getFullYear(), dt.getMonth(),
                   dt.getDate(),parseInt(e[0]), parseInt(e[1]), parseInt(e[2]));

alert( (dt >= dt1 && dt <= dt2) ? 'Current time is between startTime and endTime' : 
                                  'Current time is NOT between startTime and endTime');
alert ('dt = ' + dt  + ',  dt1 = ' + dt1 + ', dt2 =' + dt2)

回答by mohamed-ibrahim

var startTime = '15:10:10';
var endTime = '22:30:00';

currentDate = new Date()   

startDate = new Date(currentDate.getTime());
startDate.setHours(startTime.split(":")[0]);
startDate.setMinutes(startTime.split(":")[1]);
startDate.setSeconds(startTime.split(":")[2]);

endDate = new Date(currentDate.getTime());
endDate.setHours(endTime.split(":")[0]);
endDate.setMinutes(endTime.split(":")[1]);
endDate.setSeconds(endTime.split(":")[2]);


valid = startDate < currentDate && endDate > currentDate

回答by James Hay

You can possibly do something like this if you can rely on your strings being in the correct format:

如果您可以依靠正确格式的字符串,则可以执行以下操作:

var setDateTime = function(date, str){
    var sp = str.split(':');
    date.setHours(parseInt(sp[0],10));
    date.setMinutes(parseInt(sp[1],10));
    date.setSeconds(parseInt(sp[2],10));
    return date;
}

var current = new Date();

var c = current.getTime()
  , start = setDateTime(new Date(current), '15:10:10')
  , end = setDateTime(new Date(current), '22:30:00');

return (
    c > start.getTime() && 
    c < end.getTime());

回答by SamiX

A different approach: First, convert your currentDate

一种不同的方法:首先,转换您的 currentDate

var totalSec = new Date().getTime() / 1000;
var hours = parseInt( totalSec / 3600 ) % 24;
var minutes = parseInt( totalSec / 60 ) % 60;
var seconds = totalSec % 60;

var numberToCompare = hours*10000+minutes*100+seconds;

cf Convert seconds to HH-MM-SS with JavaScript?

cf使用 JavaScript 将秒数转换为 HH-MM-SS?

Then compare:

然后比较:

(numberToCompare < (endTime.split(':')[0]*10000+endTime.split(':')[1]*100+endTime.split(':')[2]*1)

or

或者

(numberToCompare > (endTime.split(':')[0]*10000+endTime.split(':')[1]*100+endTime.split(':')[2]*1)

回答by Mcm

Just another way I have for matching periods in a day, precision is in minutes, but adding seconds is trivial.

这是我在一天中匹配时间段的另一种方式,精度以分钟为单位,但添加秒数是微不足道的。

function isValid(date, h1, m1, h2, m2) {
  var h = date.getHours();
  var m = date.getMinutes();
  return (h1 < h || h1 == h && m1 <= m) && (h < h2 || h == h2 && m <= m2);
}

isValid(new Date(), 15, 10, 22, 30);