Javascript if / else无法正常工作[Time Formatting AM / PM]

时间:2015-02-13 03:19:53

标签: javascript jquery

我试图让div显示当前时间,但它总是在军事时间显示它,所以我去使用if / else语句来帮助,但它似乎不起作用。

的Javascript

var time = new Date(Date.now());
var timeHour = time.getHours();
var timeHourFix = timeHour;
var timeMinute = time.getMinutes();
var formatted = timeHourFix + ":" + timeMinute;

if(time.getHours() > 12) {
    timeHourFix = time.getHours() - 12 + "PM";
}else {
    timeHourFix = time.getHours() + "AM";
};

$( document ).ready(function() {
    $('#hourmin').text(formatted)
});

应该显示5:35 PM之类的时间,但它仍会显示17:35

6 个答案:

答案 0 :(得分:4)

那是因为您在实际修改formatted之前声明了变量timeHourFix。请尝试下面的代码。

    var time = new Date(Date.now());
    var hour = time.getHours();
    
    var t_hour = hour > 12 ? (hour - 12) : ((hour == 0) ? hour + 12 : hour);
    var formatted = t_hour + " : " + time.getMinutes() + (hour > 11 ? " PM" : " AM");

    $( document ).ready(function() {
        $('#hourmin').text(formatted)
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p id=hourmin>

答案 1 :(得分:3)

由于在设置formattedtimeHourFix之前设置了变量timeHour,因此未格式化时间。我认为在if else语句中直接设置formatted是最简单的:

var time = new Date(Date.now());
var timeHour = time.getHours();
var timeMinute = time.getMinutes();
var formatted;

if(time.getHours() > 12) {
    formatted = time.getHours() - 12 + ":" + timeMinute + " PM";
} else {
    formatted = time.getHours() + ":" + timeMinute + " AM";
};

$( document ).ready(function() {
    $('#hourmin').text(formatted)
});

答案 2 :(得分:2)

在您的情况下,问题是您在将变量timeHourFix附加到字符串后修改变量timeHourFix,字符串与AM/PM变量之间没有实时链接,因此您对变量进行了任何更改字符串连接后的变量不会反映在原始值中。

还有一些其他问题,例如timeHourFix应该在字符串的末尾,因此也必须更改。 0030还有其他问题,例如如何处理值12:30 AM,它应显示为00:30 AM而不是var time = new Date(Date.now()); var timeHour = time.getHours(); //set the hour part var timeHourFix = timeHour > 12 ? timeHour - 12 : timeHour == 0 ? 12 : timeHour; var timeMinute = time.getMinutes(); var formatted = timeHourFix + ":" + timeMinute; //set the AM/PM at the end of the string formatted += timeHour >= 12 ? ' PM' : ' AM'; $(document).ready(function() { $('#hourmin').text(formatted) });

&#13;
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="hourmin"></div>
&#13;
{{1}}
&#13;
&#13;
&#13;

答案 3 :(得分:0)

因为在将正确的值设置为timeHourFix之前设置了格式化。将格式化的分配移动到else块下面。

答案 4 :(得分:0)

这是完全正常的完整代码。

&#13;
&#13;
var time = new Date(Date.now());

var timeHour = time.getHours();

var timeHourFix = timeHour;

var timeMinute = time.getMinutes();

var formatted = timeHourFix + ":" + timeMinute;

if(time.getHours() > 12) {

    time = time.getHours() - 12 + " : " + timeMinute + " PM";

}
else {

    time = time.getHours() +" : " + timeMinute +  " AM";

};


$( document ).ready(function() {

    $('#hourmin').text(time)

});
&#13;
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>

</head>
<body>

<p id="hourmin">Time Display</p>


</body>
</html>
&#13;
&#13;
&#13;

答案 5 :(得分:0)

如果您想简化代码,可以随时执行此类操作。

var time = new Date(Date.now());
var timeHour = time.getHours();
var timeHourFix = timeHour;
var timeMinute = time.getMinutes();

$(document).ready(function() {
    var timeofday = "";    
    if(timeHour > 12) {
            timeHourFix = timeHour - 12;
            timeofday = "PM";
    }else {
        timeHourFix = timeHour;
        timeofday = "AM";
    };

    var formatted = timeHourFix + ":" + timeMinute + " " + timeofday;

   $('#hourmin').text(formatted)
});