JavaScript:如果时间 >= 9:30 那么

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

JavaScript: IF time >= 9:30 then

javascript

提问by Anthony James

I'm trying to write a statement that says "if time is this and less than that then". I can use get hours and get min. However, I'm having problems combining a time such as 9:30.

我正在尝试写一个声明,上面写着“如果时间是这个并且比那个少,那么”。我可以使用获取小时数和获取分钟数。但是,我在组合 9:30 等时间时遇到了问题。

Example,

例子,

var now = new Date();
var hour = now.getHours();
var day = now.getDay();
var mintues = now.getMinutes();

if (day == 0 && hour >= 9 && hour <= 11 && mintues >= 30) { 
    document.write(now); 
}

This only if the time is less between 9:30 10. As soon as the clock hits 10 the minutes are then < 30 and the script breaks.

仅当时间在 9:30 和 10 之间时更短。一旦时钟到达 10,分钟就会 < 30 并且脚本中断。

Any thoughts on how to better incorporate the time function to make this theory work?

关于如何更好地结合时间函数以使该理论起作用的任何想法?

Thanks,

谢谢,

回答by Josiah Ruddell

use new Date().getTime()returns milliseconds for much easier comparison. This way there is no need to check hour, min, second, millisecond. Fiddle link

使用new Date().getTime()返回毫秒以便更容易进行比较。这样就不需要检查hours, min, second, millisecond小提琴链接

var d930 = new Date(2010, 12, 21, 9, 30, 0, 0), // today 9:30:00:000
    d931 = new Date(2010, 12, 21, 9, 31, 0, 0), // today 9:31:00:000
    t930 = d930.getTime(),
    t931 = d931.getTime();


console.log(t931 > t930);

This way your code can check against a static 9:30 time.

通过这种方式,您的代码可以检查静态 9:30 时间。

var time930 = new Date(2010, 12, 21, 9, 30, 0, 0).getTime(),
    sunday = 0,
    now = new Date();

if(now.getDay() == sunday && now.getTime() >= time930){
    /* do stuff */
}

回答by Yoshiyahu

You have a few typos and basic javascript errors.
Might wanna brush up on the basics.
W3Schoolsis where I learned all I know. It works fine if you fix them...

您有一些拼写错误和基本的 javascript 错误。
可能想复习一下基础知识。
W3Schools是我学到我所知道的一切的地方。如果你修复它们,它工作正常......

var now = new Date();
  var hour = now.getHours();
  var day = now.getDay();
  var minutes = now.getMinutes();
  if(day == 0 && hour == 9 && minutes < 30 && minutes > 10 || day == 0 && hour == 9)
      document.write('Time is between 9:10 and 9:30');

Think of the if statement as basic logic.
If the day is Sunday(0)
AND the hour is 9
AND the minutes are greater than 10 AND the minutes are less than 10 OR the day is Sunday(0)
AND the hour is before 9.

将 if 语句视为基本逻辑。
如果当天是星期日(0)
并且小时是 9
并且分钟大于 10 并且分钟小于 10 或者当天是星期日(0)
并且小时在 9 之前。

回答by Funkodebat

  var now = new Date();
  var closeTime = new Date();
  closeTime.setHours(9); closeTime.setMinutes(30);
  console.log(now, closeTime, now.getTime() >= closeTime.getTime());

close time is based on today, then we just change the hours and minutes to 9:30.

关闭时间以今天为基准,那么我们只需将小时和分钟更改为 9:30。

回答by zzzzBov

if the hour is less than 9, true
or
if hour is 9 and minutes lt 30, true

so that would look like

所以看起来像

if ((hour < 9) || (hour == 9 && minutes < 30))

Use words to figure out your logic. Symbols are just shortcuts.

用文字来弄清楚你的逻辑。符号只是捷径。

回答by dmp

One way is to do a direct comparison on date objects. Choose an arbitrary year, month and day, and then incorporate your times as follows:

一种方法是对日期对象进行直接比较。选择任意的年、月和日,然后按如下方式合并您的时间:

var older = new Date("1980-01-01 12:15");
var newer = new Date("1980-01-01 12:30");

if (newer > older){
    alert("Newer time is newer");

} else {
    alert ("The time is not newer");

}

The MDC documentation on the Date objectwill help with some more details. The bottom line is that if you want to compare times, you don't actually need to call any methods on the objects, and it's possible to directly compare them. The date() object can take a variety of strings to assign a new time to the returned instance, these are from the MDC documentation:

关于 Date 对象的 MDC文档将有助于了解更多细节。最重要的是,如果你想比较时间,你实际上不需要在对象上调用任何方法,并且可以直接比较它们。date() 对象可以使用各种字符串为返回的实例分配新时间,这些来自 MDC 文档:

today = new Date();
birthday = new Date("December 17, 1995 03:24:00");
birthday = new Date(1995,11,17);
birthday = new Date(1995,11,17,3,24,0);

As you can see, it's pretty simple. Don't complicate, and have a look through the documentation :)

如您所见,这非常简单。不要复杂化,并查看文档:)

While we're here, here's a test using your example:

当我们在这里时,这是使用您的示例进行的测试:

var base = new Date("1980-01-01 9:30");
var test = new Date("1980-01-01 9:30:01");

if (test >= base){
    alert("test time is newer or equal to base time");

} else {
    alert ("test time is older than 9.30");

}

回答by Chandu

Try this:

尝试这个:

var now = new Date(); 
var hour = now.getHours(); 
var mintues = now.getMinutes();

if(
        (hour*60 + mintues) > 570 && 
        hour <= 11
    )
{
    document.write(now); 
}

回答by alev

I made this solution simple and easy to read (thus easy to adjust) by using a simple trick.

我通过使用一个简单的技巧使这个解决方案简单易读(因此易于调整)。

The trick is to transform the hour and minutes into a number. Eg 09:30into 9.30

诀窍是将小时和分钟转换为数字。例如09:30进入9.30

var now     = new Date();
var hour    = now.getHours();
var minutes = now.getMinutes();

// make the current time of day computable and readable at the same time
// 9:30 becomes 9.30
var timeOfDay = hour + (minutes / 100);

console.log('timeOfDay: ' + timeOfDay);

// For setting the time limits simply use the time with a dot instead of a colon
if( (9.30 <= timeOfDay) && (timeOfDay <= 11.30) ){
    console.log('Time is between 9:30 and 11:30');
}

回答by ngen

I don't quite fully understand your question but hope this helps.

我不太完全理解你的问题,但希望这会有所帮助。

c = new Date();
nhour  = c.getHours();
nmin   = c.getMinutes();

if(nmin <= 9) {
    nmin = "0" + nmin;
}
if(nhour <= 9) {
    nhour = "0" + nhour;
}

newtime = nhour + "" + nmin;

if(newtime <= 0930){
    alert("It is before 9:30am or earlier");
}