JS的(new Date()).toLocaleString,系统设置为24小时,仍然显示为12小时。

如题所述

JS实现12小时制和24小时制的转换。

表示时间的方式有24小时制和12小时制。如果是12小时制的时间,通常显示“上午”或“下午”。

function timeType() {
if (document.form.showTimeType[0].checked) {
//判断选择的是哪个类型-24小时就返回true
return true;
}
return false;
}
function showTheHours(theHour) {
if (timeType() || (theHour > 0 && theHour < 13)) {
//如果时间在12小时内
return (theHour);
}
if (theHour == 0) {                             
//如果时间等于0
return (12);
}
return (theHour-12);
//如果时间大于12,需要减去12-针对12小时制
}
function showZeroFilled(inValue) {
if (inValue > 9) {
//设置分钟数的两位数显示,不足两位补0
return "" + inValue;
}
return "0" + inValue;
}
function showAmPm() {                         
//显示上午或下午的方法
if (timeType()) {
return ("");
}
if (now.getHours() < 12) { 
//判断日期,显示12小时制的中文提示
return (" 上午");
}
return (" 下午");
}
function showTheTime() {   
//显示时间的方法
now = new Date         
//获取当前时间
document.form.showTime.value = showTheHours(now.getHours())
+ ":" + showZeroFilled(now.getMinutes()) + ":" +
showZeroFilled(now.getSeconds()) + showAmPm()
setTimeout("showTheTime()",1000) 
//每隔1秒更新时间
}
</script>
</head>
<BODY onLoad="showTheTime()">
<center><form name=form>
<input type=text name=showTime size=11><p>
<input type=radio name=showTimeType checked>24小时<br>
<input type=radio name=showTimeType>12小时<br>
</form></center></body>
</html>

温馨提示:答案为网友推荐,仅供参考
第1个回答  2016-06-12
(new Date()).toLocaleString()
"2016/6/12 下午4:48:18"
这不是现实是下午的4点了吗?
到24点,也会显示上午12点
建议你可以使用date.toGMTString()、date.toString()追问

不要上午、下午,要24小时!

本回答被提问者和网友采纳
第2个回答  推荐于2018-02-27
var time = new Date().toLocaleString('chinese',{hour12:false});