使用JavaScript转换12小时至24小时的格式 [英] Conversion 12-hour to 24-hour format with javascript
问题描述
我试图用24小时格式创建一个带javascript的简单时钟。据我来,我只设法获得12小时的格式。脚本实际上很简单,在这里我得到了:
< div id =updatetime>< / DIV>
< script>
setInterval(function(){
var d = new Date();
var curr_hour = d.getHours();
var curr_minute = d.getMinutes()+ 1;
document.getElementById(updatetime)。innerHTML = curr_hour +:+ curr_minute;
},1000);
< / script>
至于我的问题:如何将12小时输出转换为24小时输出仍然保持简单。
Date()。getHours()
不会以24小时格式返回时间。要从中获得12小时的时间,只需使用模数运算符( Date()。getHours()%12
)。
您应该检查您的时钟设置是否正确并且在您的时区已过了中午。
I am trying to create a simple clock with javascript in 24-hour format. As far as i've come i only managed to get a 12-hour format. The script is really simple actually, here what i've got:
<div id="updatetime"></div>
<script>
setInterval(function(){
var d = new Date();
var curr_hour = d.getHours();
var curr_minute = d.getMinutes() + 1;
document.getElementById("updatetime").innerHTML = curr_hour + ":" + curr_minute;
}, 1000);
</script>
as for my question: How do i convert the 12-hour output to a 24-hour output while still keeping it simple. Only the hours and minutes are needed.
Date().getHours()
does return the time in 24-hour format. To get 12-hour time from it, one simply uses the modulus operator (Date().getHours() % 12
).
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getHours
You should check that your clock is properly set and that it is past noon in your time zone.
这篇关于使用JavaScript转换12小时至24小时的格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!