新手"第一次约会格式"脚本

时间:2015-09-21 18:05:46

标签: javascript jquery

我昨天开始尝试使用JavaScript。这将是我的第一个脚本,但当作为html文件打开时它不能正常运行。我可以发现它有什么问题,经过四次,有什么建议吗?

<HTML>
<HEAD>
<TITLE>Displaying the current time and date (formatted version)</TITLE>
<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">

<!-- Hide from browser that do not support JavaScript

var today = new Date();

var month = today.getMonth();

var displayMonth="";

switch (month) {
   case 0 :
     displayMonth = "January"
     break
   case 1 :
     displayMonth = "February"
     break
   case 2 :
     displayMonth = "March"
     break
   case 3 :
     displayMonth = "April"
     break
   case 4 :
     displayMonth = "May"
     break 
   case 5 :
     displayMonth = "June" 
     break
   case 6 :
     displayMonth = "July"
     break 
   case 7 :
     displayMonth = "August"
     break
   case 8 :
     displayMonth = "September"
     break
   case 9 :
     displayMonth = "October"
     break 
   Case 10 :
     displayMonth = "November"
     break
   Case 11 : 
     displayMonth = "December"
     break

   default: displayMonth = "INVALID"
}

   var hours = today.getHours();
   var minutes = today.getMinutes();
   var greeting;
   var ampm;

   if (hours <= 11) {
     greeting = "Good morning!";
     ampm="a.m."; 

     if (hours == 0) {
       hours= 12;
     }
  }

     else if (hours > 11 && hours < 18) {
       greeting = "Good afternoon!";
       ampm = "p.m.";

       if (hours > 12 ) {
          hours -= 12;
       }
     }

     else if (hours > 17 && hours < 21) {
       greeting = "Good evening!";
       ampm = "p.m.";
       hours -= 12;
     }

     else if (hours > 20) {
       greeting = "Good Night!";
       ampm = "p.m.";
       hours -= 12;
     }

    if (minutes < 10) {
       minutes = "0" + minutes;
    }

var displayGreeting = displayMonth + " "
       + today.getDate() + ", "
       + today.getYear()
       + " - " + hours + ":" + minutes + " " + ampm

document.writeln(displayGreeting)  

// --> Finish hiding
</SCRIPT>
</HEAD>
<BODY>
<B> HELP <B>
</BODY>
</HTML>

3 个答案:

答案 0 :(得分:1)

欢迎,我建议不要使用如此长的case声明来获取当月,而是使用以下

var monthNames = ["January", "February", "March", "April", "May", "June",
  "July", "August", "September", "October", "November", "December"
];

var month = monthNames[today.getMonth()];

答案 1 :(得分:0)

您有两次Case而不是case,javascript是case-sensitive(没有双关语......)

你也忘了向我展示你的问候语。

一个有效的JSfiddle:https://jsfiddle.net/tg6ofrvr/2/

(我只使用jquery来显示消息,因为jsfiddle中不允许打印文档)

答案 2 :(得分:0)

你自己的代码,只是一个小小的修改。 :)

<HTML>
<HEAD>
<TITLE>Displaying the current time and date (formatted version)</TITLE>
<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
   <!-- Hide from browser that do not support JavaScript
   var today = new Date();
   var month = today.getMonth();
   var displayMonth="";
   var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
   var hours = today.getHours();
   var minutes = today.getMinutes();
   var greeting;
   var ampm;

   if (hours <= 11) {
     greeting = "Good morning!";
     ampm="a.m."; 

     if (hours == 0) {
       hours= 12;
     }
   }

     else if (hours > 11 && hours < 18) {
       greeting = "Good afternoon!";
       ampm = "p.m.";

       if (hours > 12 ) {
          hours -= 12;
       }
     }

     else if (hours > 17 && hours < 21) {
       greeting = "Good evening!";
       ampm = "p.m.";
       hours -= 12;
     }

     else if (hours > 20) {
       greeting = "Good Night!";
       ampm = "p.m.";
       hours -= 12;
     }

    if (minutes < 10) {
       minutes = "0" + minutes;
    }

    var displayGreeting = months[month] + " " + today.getDate() + ", " + today.getFullYear() + " -  " + hours + ":" + minutes + " " + ampm +"<br />"+ greeting;
    document.writeln(displayGreeting)
    // --> Finish hiding
</SCRIPT>
</HEAD>
<BODY>
<!--B> HELP <B-->
</BODY>
</HTML>