我正在尝试将以下内容转换为12小时制。

目前,我正在接收日,月,年和时区。

通过添加.toLocaleTimeString()。replace(/([[d] +:[\ d] {2})(:[\ d] {2})(。)/,“ $ 1 $ 3”)*进行修复

<div id="time1"></div>
<div id="time2"></div>


var date = new Date('08/16/2019 12:00:00 PM UTC').toLocaleTimeString().replace(/([\d]+:[\d]{2})(:[\d]{2})(.*)/, "$1$3")
document.getElementById("time1").innerHTML = date;

var date = new Date('08/16/2019 6:00:00 am UTC').toLocaleTimeString().replace(/([\d]+:[\d]{2})(:[\d]{2})(.*)/, "$1$3")
document.getElementById("time2").innerHTML = date;

最佳答案

基本上,您要做的就是使用Date()默认的javascript函数,并确保您附加了UTC时区:

var date = new Date('08/16/2019 7:00:00 PM UTC')

date.toString=() //will then print out the timezone adjusted time

"Fri Aug 16 2019 22:00:00 GMT+0300 (Eastern European Summer Time)"

09-27 04:04
查看更多