Javascript date()函数不起作用

时间:2016-07-20 08:46:46

标签: javascript

我正在学习javascript所以我正在练习javascript Date()函数。我已经在下面展示了但是这个代码不起作用,即使我打印任何下面的变量输出也没什么(空白页)。如何解决它。

var rightTime = new Date();

            var currentHour = rightTime.getHours();

            switch(true)
            {

            case (currentHour > 8 && currentHour < 12)
                document.write("Gud Morning");
                break;
            case (currentHour === 12)
                document.write("Gud After Noon");
                break;

            default:
                document.write("Gud Night");
                break;      
            }

2 个答案:

答案 0 :(得分:1)

您忘了在:

中添加case
var rightTime = new Date();

var currentHour = rightTime.getHours();

switch(true)
{

      case (currentHour > 8 && currentHour < 12):
          document.write("Gud Morning");
          break;
      case (currentHour === 12):
          document.write("Gud After Noon");
          break;
      default:
          document.write("Gud Night");
          break;      
}

答案 1 :(得分:0)

而不是:

switch(true) {

    case (currentHour > 8 && currentHour < 12)
        document.write("Gud Morning");
        break;
    case (currentHour == 12)
        document.write("Gud After Noon");
        break;

    default:
        document.write("Gud Night");
        break;      
    }

你可以写:

if(currentHour > 8 && currentHour < 12)  {
    document.write("Gud Morning");
} else if(currentHour === 12) {
    document.write("Gud After Noon");
} else {
    document.write("Gud Night");
}