Javascript日期/时间检查错误

时间:2018-05-17 13:55:41

标签: javascript angularjs wikitude

在我的应用程序中使用以下代码来显示html页面,具体取决于它是今天的日期以及当天的早上,下午或晚上的哪个时间。目前是下午2:53,代码只显示am html页面(这是第一个)。我试图运行console.log命令,但在控制台中什么都没有,这可能是因为wikitude。

获取日期的第一个功能是正常工作,只是没有正确检查时间。

var inputDate = new Date("5/17/2018");

    // Get today's date
    var todaysDate = new Date();

      // call setHours to take the time out of the comparison
      if(inputDate.setHours(0,0,0,0) == todaysDate.setHours(0,0,0,0)) {
          var hour = new Date().getHours();

          console.log("hour is: " + hour);
                    // between 12 PM and 7 AM respectively
            if(hour => 7 && hour < 12) {
                //morning   (Always running code here no matter what time of day) 
            }
            else if(hour >= 12 && hour <= 18) {
               //afternoon   
            }   
            else {

            //evening or before 7
            }
      }
      else{
           //not today (works if date is not today)
      }

1 个答案:

答案 0 :(得分:2)

if声明中有拼写错误:=>应为>=

var inputDate = new Date("5/17/2018");

// Get today's date
var todaysDate = new Date();

// call setHours to take the time out of the comparison
if (inputDate.setHours(0,0,0,0) == todaysDate.setHours(0,0,0,0)) {
  var hour = new Date().getHours();

  console.log("hour is: " + hour);
  // between 12 PM and 7 AM respectively
  if (hour >= 7 && hour < 12) {
    //morning   (Always displaying code here) 
    alert('morning')
  }
  else if (hour >= 12 && hour <= 18) {
    //afternoon
    alert('afternoon')
  }   
  else {
    //evening or before 7
    alert('evening')
  }
}
else {
  //not today
  alert('not today')
}