gpt4 book ai didi

javascript - 将 JavaScript 日期格式化为小时 :Minutes:Seconds

转载 作者:行者123 更新时间:2023-11-29 16:14:48 25 4
gpt4 key购买 nike

我有这段代码,但我无法第二次正确格式化:

setInterval(function() {
var local = new Date();
var localdatetime = local.getHours() + ":" + local.getMinutes() + ":" + local.getSeconds();

var remote = new Date();
var remotedatetime = remote.getHours() + ":" + remote.getMinutes() + ":" + remote.getSeconds();

var remoteoffset = remote.setHours(local.getHours() - 5);

$('#local-time').html(localdatetime);
$('#remote-time').html(remoteoffset);
}, 1000);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

My Time:
<div id="local-time"></div>
Their time:
<div id="remote-time"></div>

local-time 完美显示“hh:mm:ss”

remote-time 仅显示随机数列表。

如何让远程时间也变成“hh:mm:ss”?

最佳答案

您正在调整 remote 获得它的字符串表示,所以这对您没有好处。

然后您将显示 setHours() 的结果(自 1970 年 1 月 1 日以来的毫秒数)而不是字符串。

这就是我认为你的目标:

setInterval(function() {
var local = new Date();
var localdatetime = local.getHours() + ":" + pad(local.getMinutes()) + ":" + pad(local.getSeconds());

var remote = new Date();
remote.setHours(local.getHours() - 5);
var remotedatetime = remote.getHours() + ":" + pad(remote.getMinutes()) + ":" + pad(remote.getSeconds());

$('#local-time').html(localdatetime);
$('#remote-time').html(remotedatetime);
}, 1000);

function pad(t) {
var st = "" + t;

while (st.length < 2)
st = "0" + st;

return st;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

My Time:
<div id="local-time"></div>
Their time:
<div id="remote-time"></div>

关于javascript - 将 JavaScript 日期格式化为小时 :Minutes:Seconds,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18391286/

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