我的按钮上有大城市的名字 点击它们,我想获得当地时间。
$('#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?
答案 0 :(得分:29)
您应该能够检查小时数是否大于12。
var ampm = (hours >= 12) ? "PM" : "AM";
但是你在减去2之前考虑过小时小于2的情况吗?您的小时数最终会为负数。
答案 1 :(得分:20)
尝试以下代码:
$('#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);
});
答案 2 :(得分:8)
你可以这样使用,
var dt = new Date();
var h = dt.getHours(), m = dt.getMinutes();
var _time = (h > 12) ? (h-12 + ':' + m +' PM') : (h + ':' + m +' AM');
希望分钟也会更好。
答案 3 :(得分:4)
使用date.js
<script type="text/javascript" src="http://www.datejs.com/build/date.js"></script>
你可以写这样的文字
new Date().toString("hh:mm tt")
cheet sheet在这里format specifiers
tt
适用于AM/PM
答案 4 :(得分:3)
如果hours
小于12
,那就是 a.m。。
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
,现在是
hours + ' ' + am; // "6 p.m."
答案 5 :(得分:3)
非常有趣的帖子。在参数中取日期的函数中,它可以显示为:
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;
}
答案 6 :(得分:2)
试试这个:
h = h > 12 ? h-12 +'PM' : h +'AM';
答案 7 :(得分:1)
const now = new Date()
.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit', hour12: true })
.toLowerCase();
基本上,您只需要放入 {hour12:true} 就可以了。
结果=>现在=“ 21:00 pm”;
答案 8 :(得分:0)
没有扩展和复杂编码的最佳方式:
date.toLocaleString([], { hour12: true});
How do you display javascript datetime in 12 hour AM/PM format?
答案 9 :(得分: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;
答案 10 :(得分: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}`);
这将每次都适用,
答案 11 :(得分:0)
这是我在代码中使用的时间
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