jQuery 如何获得 AM 或 PM?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17003202/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 18:15:28  来源:igfitidea点击:

How to get AM or PM?

javascriptjquerydatetime

提问by qadenza

I have buttons with the names of big cities.
Clicking them, I want to get local time in them.

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

$('#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
});

But how can I get AM or PM ?

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

采纳答案by TechBytes

Try below code:

试试下面的代码:

$('#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);
});

回答by J W

You should just be able to check if hours is greater than 12.

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

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

But have you considered the case where the hour is less than 2 before you subtract 2? You'd end up with a negative number for your hour.

但是你有没有考虑过减2之前小时小于2的情况?你最终会得到一个负数。

回答by Naren Srinivasan S

You can use like this,

你可以这样使用,

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

Hopes this will be better with minutes too.

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

回答by Sameera R.

with date.js

使用 date.js

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

you can write like this

你可以这样写

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

cheet sheet is here format specifiers
ttis for AM/PM

cheet 表在这里格式说明符
tt用于AM/PM

回答by jeremy-denis

very interesting post. in a function that take a date in parameter it can appear like that :

非常有趣的帖子。在参数中采用日期的函数中,它可能如下所示:

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;

}

回答by Paul S.

If hoursis less than 12, it's the a.m..

如果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;

Now, for me as you didn't use getUTCHours, it is currently 2 hours after

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

hours + ' ' + am; // "6 p.m."

回答by elclanrs

Try this:

尝试这个:

h = h > 12 ? h-12 +'PM' : h +'AM';

回答by Edwin Mendez

The best way without extensions and complex coding:

没有扩展和复杂编码的最佳方式:

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

How do you display javascript datetime in 12 hour AM/PM format?

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

回答by Vik2696

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;

回答by Prabhat kumar

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}`);

This will work for everytime,

这将适用于每次,