当我在javascript中使用“getHour()”方法时,它会显示军事时间格式。我需要它来显示1-12之间的小时数。谁能告诉我怎么做?这是我正在使用的代码:
function updateclock()
{
var time = new Date();
var todisplay = '';
if (time.getHours() < 10) todisplay += time.getHours();
else todisplay += time.getHours();
if (time.getMinutes() < 10) todisplay += ':0' + time.getMinutes();
else todisplay += ':' + time.getMinutes();
document.getElementById("clock").innerHTML = todisplay;
}
答案 0 :(得分:33)
为什么不以简短的方式做到这一点?数学,人! :)
// returns the hours number for a date, between 1 and 12
function hours12(date) { return (date.getHours() + 24) % 12 || 12; }
答案 1 :(得分:32)
这将使13 - 24回到1 - 12的范围,0回到12:
var hours = time.getHours();
if (hours > 12) {
hours -= 12;
} else if (hours === 0) {
hours = 12;
}
此外,您需要停止在代码中重复自己。致电time.getHours()
和time.getMinutes()
并将其值仅存储一次,然后担心添加前导零,例如:
function updateclock() {
function pad(n) {
return (n < 10) ? '0' + n : n;
}
var time = new Date();
var hours = time.getHours();
var minutes = time.getMinutes();
if (hours > 12) {
hours -= 12;
} else if (hours === 0) {
hours = 12;
}
var todisplay = pad(hours) + ':' + pad(minutes);
document.getElementById("clock").innerHTML = todisplay;
}
答案 2 :(得分:8)
function getClockTime(){
var now = new Date();
var hour = now.getHours();
var minute = now.getMinutes();
var second = now.getSeconds();
var ap = "AM";
if (hour > 11) { ap = "PM"; }
if (hour > 12) { hour = hour - 12; }
if (hour == 0) { hour = 12; }
if (hour < 10) { hour = "0" + hour; }
if (minute < 10) { minute = "0" + minute; }
if (second < 10) { second = "0" + second; }
var timeString = hour + ':' + minute + ':' + second + " " + ap;
return timeString;
}
此功能将在1-12小时内提供完美的时间格式
答案 3 :(得分:4)
其他答案确实非常好。 但我认为,也可以包含以下内容。
var d = new Date();
var hour = d.getHours();
var minute = d.getMinutes();
var fulltime = "";
// create a 24 elements(0-23) array containing following values
const arrayHrs = [12,1,2,3,4,5,6,7,8,9,10,11,12,1,2,3,4,5,6,7,8,9,10,11];
// since getMinutes() returns 0 to 9 for upto 9 minutes, not 00 to 09, we can do this
if(minute < 10) {
minute = "0" + minute;
}
if( hour < 12) {
// Just for an example, if hour = 11 and minute = 29
fulltime = arrayHrs[hour] + ":" + minute + " AM"; // fulltime = 11:29 AM
}
else {
// similarly, if hour = 22 and minute = 8
fulltime = arrayHrs[hour] + ":" + minute + " PM"; // fulltime = 10:08 PM
}
看看我在arrayHrs中做了什么;)
答案 4 :(得分:1)
使用它的简单方法。
setInterval(function() {
var d = new Date();
document.getElementById("demo").innerHTML =
d.getHours() % 12+" : "+d.getMinutes()+" : "+d.getSeconds();
}, 1000);
&#13;
<p id="demo" ></p>
&#13;
答案 5 :(得分:1)
最短距离:
const hours12 = date => (date.getHours() % 12 || 12);
如果您需要填充0:
const hours12 = date => ("0"+(date.getHours() % 12 || 12)).slice(-2);
还有AM和PM的另一个选项:
const hours12 = date => date.toLocaleString('en-US', { hour: 'numeric', hour12: true })
答案 6 :(得分:0)
getDateTime = () => {
const today = new Date();
const day = today.toLocaleDateString('en-us', { weekday: 'short' });
const month = today.toLocaleString('en-us', { month: 'short' });
const date = today.getDate()
const year = today.getFullYear()
const hours = today.getHours()
const minutes = today.getMinutes().toString()
var dayORnight = "AM";
if (hours > 11) { dayORnight = "PM"; }
if (hours > 12) { hours = hours - 12; }
if (hours == 0) { hours = 12; }
if (hours < 10) { hours = "0" + hours; }
if (minutes < 10) { minutes = "0" + minutes; }
const datetime = `${day}, ${month} ${date}, ${year} at ${hours}:${minutes} ${dayORnight}`;
console.log(datetime)
}
答案 7 :(得分:0)
这会将 24 小时时间转换为 12 小时,并且还添加了 AM 和 PM 后缀。
const hours12 = (date) => {
let ampm = date.getHours() >= 12 ? 'pm' : 'am';
return `${(date.getHours() % 12 || 12)} ${ampm}`;
};
console.log(hours12(new Date()));
@chickens 的修改版本。
答案 8 :(得分:0)
这可以使用 .getHours() 函数轻松实现。请记住,getHours 返回 0 到 23 之间的整数值,因此您要做的就是检查 0 值
let date = new Date()
let hour = (date.getHours()===0)? 24:date.getHours()
答案 9 :(得分:-1)
类似于已接受但没有||
运算符的答案。
纯粹算术:
// returns the hours number for a date, between 1 and 12
function hours12(date) { return (date.getHours() + 11) % 12 + 1; }
在这里进行测试。模拟0-23小时:
var demoTable = document.getElementById("demo");
// simulate getHours() values 0-23
for (i = 0; i < 24; i++) {
var row = demoTable.insertRow(-1);
var format24Cell = row.insertCell(0);
var conversionCell = row.insertCell(1);
var format12Cell = row.insertCell(2);
format24Cell.innerHTML = i;
conversionCell.innerHTML = "( <b>" + i + "</b> + 11) % 12 + 1 =";
// conversion takes place here
format12Cell.innerHTML = (i + 11) % 12 + 1;
}
table {
border-collapse: collapse;
width: 100%;
}
table,
th,
td {
border: 1px solid black;
}
th {
padding: 0, 5px
}
td {
padding-left: 5px;
}
<table id="demo">
<tr>
<th colspan="3">24 to 12 Hour Format Conversion Demo</th>
<tr>
<tr>
<th>24 Hour Format</th>
<th>Conversion</th>
<th>12 Hour Format</th>
</tr>
</table>