日期和时间脚本

时间:2017-08-15 10:47:20

标签: javascript date time

我正在尝试从JS初学者那里制作一个日期和时间脚本,我相信你们之前有人已经看过这个。

我不确定我哪里出错了,并且我正在努力解决问题,因为日志只是吐出'错误'。

任何人都可以为我清楚,坚持我编写脚本的一般方式。解释会很棒!

var date = new Date();

var day = date.getDay();
var hour = date.getHours();
var min = date.getMinutes();
var sec = date.getSeconds();

var weekdays = [Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday];
var today = weekdays[day];

var amPm = amPmFunc;
function amPmFunc() {
    if(hour < 12) {
        amPm = AM;
    } else { 
        amPm = PM;
    }
}

console.log("Today is: " + today);
console.log("Current time is: " + hour + amPM + ":" + min + ":" + "sec");

5 个答案:

答案 0 :(得分:3)

首先,您忘记用''括起一些字符串。即Sunday应为'Sunday'

另外 - 请记住 - Javascript 具有案例性。因此,名为amPm的变量将不会被识别为amPM

此外,您的函数amPmFunc应返回一个值,而不是设置全局变量的值。它只是这样整洁。当你打电话给它时,用括号称呼它:var amPm = amPmFunc();

但除此之外 - 干得好! ☻

见这个固定的例子:

&#13;
&#13;
var date = new Date();

var day = date.getDay();
var hour = date.getHours();
var min = date.getMinutes();
var sec = date.getSeconds();

var weekdays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
var today = weekdays[day];

var amPm = amPmFunc();

function amPmFunc() {
    if(hour < 12) 
        return 'AM';
    else 
        return 'PM';
}

console.log("Today is: " + today);
console.log("Current time is: " + hour + amPm + ":" + min + ":" + "sec");
&#13;
&#13;
&#13;

答案 1 :(得分:3)

为什么?

  1. Array declaration 错误。请注意格式化数组中字符串的引号
  2. Javascript是case sensitive one。所以不要忘记信的情况
  3. 对于函数声明,您可以使用()之类的var amPm = amPmFunc()。并在ampm函数上添加return statement,否则返回undefined。
  4.   

    我的建议:为了更好地学习如何申报variable,功能和   js中的字符串

    var date = new Date();
    
    var day = date.getDay();
    var hour = date.getHours();
    var min = date.getMinutes();
    var sec = date.getSeconds();
    
    var weekdays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
    var today = weekdays[day];
    
    var amPm = amPmFunc()
    
    function amPmFunc() {
      if (hour < 12) {
         amPm = 'AM';
      } else {
        amPm = 'PM';
      }
      return amPm
    }
    
    console.log("Today is: " + today);
    console.log("Current time is: " + hour + amPm + ":" + min + ":" + "sec");

答案 2 :(得分:1)

<body>
      <nav class="navbar navbar-default">
      <div class="container-fluid-nav text-center">
            <ul class="nav navbar-nav">
               <li><a href="#">CSGO</a>
               </li>
               <li><a href="#">ARMA III</a>
               </li>
               <li><a href="#">PUBG</a>
               </li>
               <li><a href="#">Other</a>
               </li>
               <li><a href="#">About</a>
               </li>
               <li><a href="#">External Links</a>
               </li>
            </ul>
      </div>
      </nav>
      <div class="footer navbar-fixed-bottom">
         <p class="footer-text">Copyright 2017 ©</p>
      </div>
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
      <script src="script.js"></script>
      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
      <script src="js/bootstrap.min.js"></script>
   </body>

他们应该是字符串。

var weekdays = [Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday];

答案 3 :(得分:1)

  1. 所有字符串必须位于&#34;
  2. name_of_function();而是name_of_function
  3. 函数必须返回某些东西,你的amPmFunc()没有返回amPm
  4.     var date = new Date();
    
    var day = date.getDay();
    var hour = date.getHours();
    var min = date.getMinutes();
    var sec = date.getSeconds();
    
    var weekdays = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
    var today = weekdays[day];
    
    
    function amPmFunc() {
        if(hour < 12) {
            amPm = "AM";
        } else { 
            amPm = "PM";
        }
    
        return amPm;
    }
    var amPM = amPmFunc();
    console.log("Today is: " + today);
    console.log("Current time is: " + hour + amPM + ":" + min + ":" + "sec");
    

答案 4 :(得分:0)

试试这个

var date = new Date();
var day = date.getDay();
var hour = date.getHours();
var min = date.getMinutes();
var sec = date.getSeconds();

var weekdays = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
var today = weekdays[day];

var amPm = '';
    if(hour < 12) {
        amPm = 'AM';
    } else if (hour > 12){ 
        hour = hour - 12;
        amPm = 'PM';
    } else {
        amPm = 'PM'
    }
console.log("Today is: " + today);
console.log("Current time is: " + hour + amPm + ":" + min + ":" + sec);