gpt4 book ai didi

javascript - 我如何获取两个本地日期时间之间的确切差异,并且时间应该像秒表一样每秒更新一次?

转载 作者:行者123 更新时间:2023-12-03 00:10:44 25 4
gpt4 key购买 nike

实际上我想获得两个本地日期和时间之间的确切时间差,并且时间应该每秒更新一次?我用了数学。计算小时、分钟、秒和天的函数这是我使用的代码。

<html>
<div class="form-group">
<label for="exampleInputName" style="color: #ffffff;">Start Time</label>
<input id="sdt" value="" type="datetime-local" class="form-control" name="Sdt">
</div>
<div class="form-group">
<label for="exampleInputName" style="color: #ffffff;">End Time</label>
<input id="edt" value="" type="datetime-local" class="form-control" name="Edt">
</div>

<input id="btnAdded" onclick="st()" type="button" class="btn btn-primary btn-block" name="sub" value="Start" style="width:100px;width:100px; margin-left:10px;display:flex;justify-content: center;border-radius: 20px;">

<p id="demo" style=" text-align: center;
font-size: 60px;
margin-top: 0px; margin-left: 170px;
float: left;"></p>

<script>
function st()
{
var endtime = document.getElementById("edt").value;
var x = setInterval(function() {

var starttime = document.getElementById("sdt").value;
var distance = endtime - starttime;



// Time calculations for days, hours, minutes and seconds
var days = Math.floor(distance / (1000 * 60 * 60 * 24));
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);

// Output the result in an element with id="demo"
document.getElementById("demo").innerHTML = days + "d " + hours + "h "
+ minutes + "m " + seconds + "s ";

// If the count down is over, write some text
if (distance < 0) {
clearInterval(x);
document.getElementById("demo").innerHTML = "EXPIRED";
}
}, 1000);}
</script>

最佳答案

你可以试试这个。我对代码进行了一些重构。

<div class="form-group">
<label for="exampleInputName" style="color: #ffffff;">Start Time</label>
<input id="sdt" value="" type="datetime-local" class="form-control" name="Sdt">
</div>
<div class="form-group">
<label for="exampleInputName" style="color: #ffffff;">End Time</label>
<input id="edt" value="" type="datetime-local" class="form-control" name="Edt">
</div>

<input id="btnAdded" onclick="st()" type="button" class="btn btn-primary btn-block" name="sub" value="Start" style="width:100px;width:100px; margin-left:10px;display:flex;justify-content: center;border-radius: 20px;">

<p id="demo" style="text-align: center;font-size: 60px;margin-top: 0px; margin-left: 170px;float: left;"></p>


<script>
function getVals (diff) {
var days = Math.floor(diff / (24 * 60 * 60));
var hours = Math.floor(diff / (60 * 60)) % 24;
var minutes = Math.floor(diff / 60) % 60;
var secs = diff % 60;
return {
days, hours, minutes, secs,
};
}
function st() {
var endtime = document.getElementById("edt").valueAsNumber;
var starttime = document.getElementById("sdt").valueAsNumber;
var distance = (endtime - starttime)/1000;
var x = setInterval(function() {
distance -= 1;
// Time calculations for days, hours, minutes and seconds

var time = getVals(distance);

// Output the result in an element with id="demo"
document.getElementById("demo").innerHTML = time.days + "d " + time.hours + "h "
+ time.minutes + "m " + time.secs + "s ";

// If the count down is over, write some text
if (distance < 0) {
console.log('Comes here');
clearInterval(x);
document.getElementById("demo").innerHTML = "EXPIRED";
}
}, 1000);
}
</script>

关于javascript - 我如何获取两个本地日期时间之间的确切差异,并且时间应该像秒表一样每秒更新一次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54742317/

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