试图弄清楚当前时间和日期代码的逻辑

时间:2015-09-10 22:01:55

标签: javascript

var today = new Date();
var day = today.getDay();
var daylist = ["Sunday","Monday","Tuesday","Wednesday ","Thursday","Friday","Saturday"];
       console.log("Today is : " + daylist[day] + ".");
        var hour = today.getHours();
        var minute = today.getMinutes();
        var second = today.getSeconds();
        var prepand = (hour >= 12)? " PM ":" AM ";
        hour = (hour >= 12)? hour - 12: hour;

if (hour===0 && prepand===' PM ') 
{    
if (minute===0 && second===0)
{  
hour=12;
prepand=' Noon';
}  
else
{  
hour=12;
prepand=' PM';
}  
}

if (hour===0 && prepand===' AM ') 
{    
if (minute===0 && second===0)
{  
hour=12;
prepand=' Midnight';
}  
else
{  
hour=12;
prepand=' AM';
}  
}  


console.log("Current Time : "+hour + prepand + " : " + minute + " : " + second);

我正在试图弄清楚if语句在这段代码中是如何工作的。这对我没有任何意义。请看下面的评论。对于不同的时间段,为什么小时= 12为所有?我觉得这部分完全迷失了。

if (hour===0 && prepand===' PM ')    
{    
if (minute===0 && second===0)
{  
hour=12;  //when it is noon, the hour will be 12, and prepand will be "pm"...but why here it is still 12? 
prepand=' Noon';
}  
else
{  
hour=12;
prepand=' PM';
}  
}


if (hour===0 && prepand===' AM ') 
{    
if (minute===0 && second===0)
{  
hour=12;    //isn't when it is 24 for midnight? why 12 again? 
prepand=' Midnight';
}  
else
{  
hour=12;    //and why it is still 12 for else? 
prepand=' AM';
}  
}  

3 个答案:

答案 0 :(得分:1)

代码通过将24-hour clock format(由世界上大多数人和JavaScript使用)的日期/时间格式化为12-hour clock format(在美国使用)来确定是中午还是午夜

以下是您的代码的简化版本,其中包含一些注意事项:

{{1}}

答案 1 :(得分:1)

基本上,只有当时间正好是午夜或中午时,脚本才会尝试显示MidnightNoon,这意味着分钟和秒都等于0.逻辑过于复杂,可以归结为一些更具可读性和简洁性的内容,例如:

  if (hour === 0) {
    hour = 12;

    if (minute === 0 && second === 0)
      prepend = prepend === ' AM ' ? ' Midnight ' : ' Noon ';
  }

这是一段代码段。注意MidnightNoon仅在没有分钟或秒时显示。



function formatDate(date) {
  var day = date.getDay(),
    daylist = ["Sunday", "Monday", "Tuesday", "Wednesday ", "Thursday", "Friday", "Saturday"],
    hour = date.getHours() % 12,
    minute = date.getMinutes(),
    second = date.getSeconds(),
    prepend = hour === date.getHours() ? ' AM ' : ' PM ';

  if (hour === 0) {
    hour = 12;

    if (minute === 0 && second === 0)
      prepend = prepend === ' AM ' ? ' Midnight ' : ' Noon ';
  }

  return daylist[day] + ' ' + hour + ':' + ('00' + minute).substr(-2) + ':' + ('00' + second).substr(-2) + prepend
}

/// Setup for demo
var _input = document.getElementById('inp'),
    _btn = document.getElementById('btn'),
    _output = document.getElementById('out');

_btn.addEventListener('click', function() {
  var msg = 'Not a valid date';
  
  if (!isNaN(Date.parse(_input.value)))
    msg = formatDate(new Date(_input.value));
  
  _output.innerText = msg;
})

<input id='inp' value='2015/01/01 00:00:00' />
<button type='button' id='btn'>Go</button>
<div id='out'></div>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

您的代码没有任何意义

if (hour===0 && prepand===' PM ') 

prepand早已定义为:

var prepand = (hour >= 12)? " PM ":" AM ";

所以逻辑prepand" PM " hour >= 12" AM " hour < 12

并且if (hour===0 && prepand===' PM ')将永远不会触发,因为它不能同时hour 0和prepand“PM”,因为JS的方法.getHours()返回0-23的值

此外,让您的生活更轻松,避免像" AM "" AM"

这样的技巧