javascript 如何使用javascript将时间从24小时格式转换为12小时格式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9497724/
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
How to convert time from 24 hour format to 12 hour format using javascript?
提问by saran
The function returns the time in 24 hour format.
该函数以 24 小时格式返回时间。
function fomartTimeShow(h) {
return h < 10 ? "0" + h + ":00" : h + ":00";
}
returns the time in 24 hour format. I want the time to be converted in 12 hour format.
Anyhelp would be greatly appreciated.
Thanks.
以 24 小时格式返回时间。我希望将时间转换为 12 小时格式。
任何帮助将不胜感激。
谢谢。
回答by Ben Lee
Just use modulus 12:
只需使用模数 12:
function formatTimeShow(h_24) {
var h = h_24 % 12;
return (h < 10 ? '0' : '') + h + ':00';
}
Modulus (%
) means divide and take remainder. For example 17 / 12 = 1 with remainder 5. So the result of 17 % 12 is 5. And hour 17 is hour 5 in 12-hour time.
Modulus ( %
) 表示除以取余。例如 17 / 12 = 1 余数为 5。所以 17 % 12 的结果是 5。17 小时是 12 小时的第 5 小时。
But note that this function is not complete since it doesn't work for hour 0 (or hour 12). To fix it you have to add in another check for that:
但请注意,此功能并不完整,因为它在 0 小时(或 12 小时)内不起作用。要修复它,您必须为此添加另一项检查:
function formatTimeShow(h_24) {
var h = h_24 % 12;
if (h === 0) h = 12;
return (h < 10 ? '0' : '') + h + ':00';
}
Also note that you can add a meridian easily, by seeing whether the hour is less than 12 (am) or equal to/greater (pm):
另请注意,您可以通过查看小时是否小于 12 (am) 或等于/大于 (pm) 来轻松添加子午线:
function formatTimeShow(h_24) {
var h = h_24 % 12;
if (h === 0) h = 12;
return (h < 10 ? '0' : '') + h + ':00' + (h_24 < 12 ? 'am' : 'pm');
}
Note: All of the above is assuming the parameter to this function is an integer between 0 and 23.
注意:以上所有内容均假设此函数的参数是 0 到 23 之间的整数。
回答by K Griffiths
You could try something like the following:
您可以尝试以下操作:
function fomartTimeShow(h) {
var ampm = "am"
if (h >= 12)
ampm = "pm"
h = h % 12;
if (h == 0)
h = 12;
return h ":00" + ampm;
}
回答by Diode
function fomartTimeShow(h) {
var s = (h % 24 < 12) ? "am" : "pm", h = h % 12 || 12;
return (h < 10 ? "0" + h : h) + ":00" + " " + s;
}
回答by Ravi Agrawal
In case if you are getting time like this "16:26", "12:50", "05:23", etc. Then, you can easily convert them into 12 hour format like "4:26 PM", "12:50 PM", "05:23 AM", etc. with AM/PM through this function.
如果您有这样的时间"16:26", "12:50", "05:23"等,那么您可以轻松地将它们转换为 12 小时格式,如"4:26 PM"、"12 :50 PM", "05:23 AM"等通过此功能与 AM/PM。
function convertTo12Hour(oldFormatTime) {
console.log("oldFormatTime: " + oldFormatTime);
var oldFormatTimeArray = oldFormatTime.split(":");
var HH = parseInt(oldFormatTimeArray[0]);
var min = oldFormatTimeArray[1];
var AMPM = HH >= 12 ? "PM" : "AM";
var hours;
if(HH == 0){
hours = HH + 12;
} else if (HH > 12) {
hours = HH - 12;
} else {
hours = HH;
}
var newFormatTime = hours + ":" + min + " " + AMPM;
console.log(newFormatTime);
}
回答by BobH
I'm pretty sure this will work as an even more concise formulaic version of Ben Lee's answer, including for the h=0 and h=12 cases:
我很确定这将作为 Ben Lee 答案的更简洁的公式版本起作用,包括 h=0 和 h=12 情况:
function fomartTimeShow(h_24) {
var h = ((h_24 + 11) % 12)+1;
return (h < 10 ? '0' : '') + h + ':00';
}
or including am/pm:
或包括上午/下午:
function fomartTimeShow(h_24) {
var h = ((h_24 + 11) % 12)+1;
return (h < 10 ? '0' : '') + h + ':00' + (h_24 < 12 ? 'am' : 'pm');
}