如何将我的 Javascript 数字时钟从军用时间更改为标准时间?

How Do I Change My Javascript Digital Clock from Military Time to Standard Time?

我用 Javascript 在我的网站上创建了一个数字时钟,并想将它从军用时间更改为标准时间。我只希望它也显示小时和分钟。我应该怎么做才能实现它?

body {
  background-color: black;
  margin-left: 5%;
  margin-right: 5%;
}

#txt {
  color: white;
  float: left;
  font-family: OpenSans;
  font-size: 90px;
  margin: 20px;
}

#weather {
  color: white;
  float: right;
  font-family: OpenSans;
  font-size: 40px;
  margin: 20px;
}
<!DOCTYPE html>
<html>
  <head>
    <title>Blooming Time And Temperature</title>
    <link href="css/format.css" rel="stylesheet"/>
    <link href="https://fonts.googleapis.com/css?family=Open+Sans" rel="stylesheet">
    <script>
    function startTime() {
        var today = new Date();
        var h = today.getHours();
        var m = today.getMinutes();
        var s = today.getSeconds();
        m = checkTime(m);
        s = checkTime(s);
        document.getElementById('txt').innerHTML =
        h + ":" + m + ":" + s;
        var t = setTimeout(startTime, 500);
    }
    function checkTime(i) {
        if (i < 10) {i = "0" + i};  // add zero in front of numbers < 10
        return i;
    }
    </script>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
    <script src="js/jquery.simpleWeather.min.js"></script>
    <script>
      $(document).ready(function() {
        $.simpleWeather({
          location: 'Brooklyn, NY',
          woeid: '',
          unit: 'f',
          success: function(weather) {
            html = '<p>'+weather.temp+'&deg;'+weather.units.temp+'</p>';
            html += '<div id="city">'+weather.city+', '+weather.region+'</div>';

            $("#weather").html(html);
          },
          error: function(error) {
            $("#weather").html('<p>'+error+'</p>');
          }
        });
      });
    </script>
  </head>
  <body onload="startTime()">
    <div id="txt"></div>
    <div id="weather"></div>
  </body>
</html>

j08691 的评论是正确的,但我将添加 AM/PM 信息:

var AMPM = (h >= 12)? " PM":" AM";
if (h > 12) h = h - 12;
if (h < 1) h = h + 12;
document.getElementById('txt').innerHTML = h + ":" + m + ":" + s + AMPM;

如果你愿意使用像moment这样的插件,那就很简单了。

MomentJs 是一个非常方便和有用的插件来处理日期和时间。

var timeOut;
function startTime() {
  document.getElementById('txt').innerHTML =
    moment().format("hh:mm:ss A");
  clearTimeout(timeOut);
  timeOut = setTimeout(startTime, 1000);
}

$(document).ready(function() {
  startTime();
  $.simpleWeather({
    location: 'Brooklyn, NY',
    woeid: '',
    unit: 'f',
    success: function(weather) {
      html = '<p>' + weather.temp + '&deg;' + weather.units.temp + '</p>';
      html += '<div id="city">' + weather.city + ', ' + weather.region + '</div>';

      $("#weather").html(html);
    },
    error: function(error) {
      $("#weather").html('<p>' + error + '</p>');
    }
  });
});
body {
  background-color: black;
  margin-left: 5%;
  margin-right: 5%;
}
#txt {
  color: white;
  float: left;
  font-family: OpenSans;
  font-size: 90px;
  margin: 20px;
}
#weather {
  color: white;
  float: right;
  font-family: OpenSans;
  font-size: 40px;
  margin: 20px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.15.1/moment.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.simpleWeather/3.1.0/jquery.simpleWeather.min.js"></script>
<div id="txt"></div>
<div id="weather"></div>