JS的系统设置为24小时,仍然显示为12小时是怎么回事?

JavaScript08

JS的系统设置为24小时,仍然显示为12小时是怎么回事?,第1张

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>

var dt1 = "2009-11-5 10:30"

var dt2 = "2009-11-8 9:20"

var regTime = /(\d{4})-(\d{1,2})-(\d{1,2})( \d{1,2}:\d{1,2})/g

//alert(dt1.replace(regTime, "$2-$3-$1$4"))

var interval = Math.abs(Date.parse(dt1.replace(regTime, "$2-$3-$1$4")) - Date.parse(dt2.replace(regTime, "$2-$3-$1$4")))/1000

var h = Math.floor(interval / 3600)

var m = Math.floor(interval % 3600 / 60)

alert(h + " 小时 " + m + " 分")

在IOS机型上遇到一个获取日期中的小时数莫名其妙多了8个小时的BUG,最后原因为new Date()传入的日期格式不对。

原日期格式:2019-07-10T14:00:00;但在IOS上面需要的格式:2019/07/10T14:00:00;

这时把date用在new Date(date)里面,再getHours()就不会再多8小时了。