本文介绍了JavaScript:IF时间> = 9:30然后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试写一个声明,说如果时间是这个而且比那时少。我可以使用get hours并获得分钟。但是,我在组合时间如9:30时遇到了问题。

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.

示例,

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); 
}

这只有在9:30之间的时间内减少10时。时钟点击10分钟然后< 30,脚本中断。

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.

如何更好地整合时间函数以使这个理论有效?

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

谢谢,

推荐答案

使用 new Date()。getTime()返回毫秒,以便更容易比较。这样就无需检查小时,分钟,秒,毫秒

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

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);

这样你的代码可以在9:30的时间内检查静态。

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

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 */
}

这篇关于JavaScript:IF时间&gt; = 9:30然后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 02:28