37

我有大城市名称的按钮。
单击它们,我想在它们中获取当地时间。

$('#btnToronto').click(function () {
    var hours = new Date().getHours();
    var hours = hours-2; //this is the distance from my local time
    alert ('Toronto time: ' + hours + ' h'); //this works correctly
});

但是我怎样才能得到 AM 或 PM 呢?

4

13 回答 13

41

您应该能够检查小时是否大于 12。

var ampm = (hours >= 12) ? "PM" : "AM";

但是在减去 2 之前,您是否考虑过小时小于 2 的情况?您的小时数最终会为负数。

于 2013-06-08T19:55:22.520 回答
23

试试下面的代码:

$('#btnToronto').click(function () {
    var hours = new Date().getHours();
    var hours = (hours+24-2)%24; 
    var mid='am';
    if(hours==0){ //At 00 hours we need to show 12 am
    hours=12;
    }
    else if(hours>12)
    {
    hours=hours%12;
    mid='pm';
    }
    alert ('Toronto time: ' + hours + mid);
});
于 2013-06-08T19:56:21.450 回答
13

你可以这样使用,

var dt = new Date();
    var h =  dt.getHours(), m = dt.getMinutes();
    var _time = (h > 12) ? (h-12 + ':' + m +' PM') : (h + ':' + m +' AM');

希望这也会在几分钟内变得更好。

于 2015-01-09T05:33:05.857 回答
9
const now = new Date()
      .toLocaleTimeString([], { hour: '2-digit', minute: '2-digit', hour12: true })
      .toLowerCase();

基本上你只需要输入{hour12: true}就可以了。

结果 => 现在 = "21:00 pm";

于 2020-08-25T02:36:27.087 回答
4

如果hours小于12,则为am

var hours = new Date().getHours(), // this is local hours, may want getUTCHours()
    am;
// adjust for timezone
hours = (hours + 24 - 2) % 24;
// get am/pm
am = hours < 12 ? 'a.m.' : 'p.m.';
// convert to 12-hour style
hours = (hours % 12) || 12;

现在,对我来说,因为你没有使用getUTCHours,现在是 2 小时后

hours + ' ' + am; // "6 p.m."
于 2013-06-08T19:51:51.323 回答
4

非常有趣的帖子。在参数中采用日期的函数中,它可能看起来像这样:

function hourwithAMPM(dateInput) {
   var d = new Date(dateInput);
   var ampm = (d.getHours() >= 12) ? "PM" : "AM";
   var hours = (d.getHours() >= 12) ? d.getHours()-12 : d.getHours();

   return hours+' : '+d.getMinutes()+' '+ampm;

}
于 2016-05-31T19:29:13.700 回答
3

使用 date.js

<script type="text/javascript" src="http://www.datejs.com/build/date.js"></script>

你可以这样写

new Date().toString("hh:mm tt")

cheet sheet is here格式说明符
tt用于AM/PM

于 2015-10-01T11:10:12.563 回答
2

尝试这个:

h = h > 12 ? h-12 +'PM' : h +'AM';
于 2013-06-08T19:52:21.717 回答
1

无需扩展和复杂编码的最佳方式:

date.toLocaleString([], { hour12: true});

如何以 12 小时 AM/PM 格式显示 javascript 日期时间?

于 2017-01-25T13:47:04.813 回答
1

这是我在代码中使用的时间

let current = new Date();
let cDate = current.getDate() + '-' + (current.getMonth() + 1) + '-' + current.getFullYear();
let hours = current.getHours();
let am_pm = (hours >= 12) ? "PM" : "AM";

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

let cTime = hours + ":" + current.getMinutes() + ":" + current.getSeconds() +" "+ am_pm;
let dateTime = cDate + ' ' + cTime;

console.log(dateTime);   // 1-3-2021 2:28:14 PM
于 2021-03-01T09:30:05.137 回答
0
var now = new Date();
var hours = now.getHours();
var minutes = now.getMinutes();
var ampm = hours >= 12 ? 'pm' : 'am';
hours = hours % 12;
hours = hours ? hours : 12;
minutes = minutes < 10 ? '0' + minutes : minutes;
var timewithampm = hours + ':' + minutes + ' ' + ampm;
return timewithampm;
于 2019-10-01T10:24:01.583 回答
0

function Timer() {
    var dt = new Date()
    if (dt.getHours() >= 12){
        ampm = "PM";
    } else {
        ampm = "AM";
    }
    if (dt.getHours() < 10) {
        hour = "0" + dt.getHours();
    } else {
        hour = dt.getHours();
    }
    if (dt.getMinutes() < 10) {
        minute = "0" + dt.getMinutes();
    } else {
        minute = dt.getMinutes();
    }
    if (dt.getSeconds() < 10) {
        second = "0" + dt.getSeconds();
    } else {
        second = dt.getSeconds();
    }
    if (dt.getHours() > 12) {
        hour = dt.getHours() - 12;
    } else {
        hour = dt.getHours();
    }
    if (hour < 10) {
        hour = "0" + hour;
    } else {
        hour = hour;
    }
    document.getElementById('time').innerHTML = hour + ":" + minute + ":" + second + " " + ampm;
    setTimeout("Timer()", 1000);
}
Timer()
<div id="time"></div>

于 2021-11-11T11:15:40.347 回答
0
var dt = new Date();
var h = dt.getHours(),
  m = dt.getMinutes();
var time;
if (h == 12) {
  time = h + ":" + m + " PM";
} else {
  time = h > 12 ? h - 12 + ":" + m + " PM" : h + ":" + m + " AM";
}
//var time = h > 12 ? h - 12 + ":" + m + " PM" : h + ":" + m + " AM";

console.log(`CURRENT TIME IS ${time}`);

这将适用于每次,

于 2020-05-04T06:59:41.513 回答