gpt4 book ai didi

javascript - 如何将 Date 对象转换为仅输出 hh :mm am/pm

转载 作者:行者123 更新时间:2023-12-02 23:07:19 25 4
gpt4 key购买 nike

我正在尝试将以下内容转换为 12 小时 am/pm 格式。

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

通过添加 .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)"

关于javascript - 如何将 Date 对象转换为仅输出 hh :mm am/pm,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57529503/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com