我有一個倒計時計時器,我想為每個用戶顯示完全相同的剩余小時數和分鐘數,而不管時區或位置如何(天數并不重要)。
我假設我需要以某種方式定位和輸出 UTC。我會有任何夏令時問題嗎?實際結束時間不是很重要。每個人看到的剩余時間都是一樣的。
我在這里研究過類似的帖子,但它們已經有幾年歷史了,沒有找到答案。希望有解決方案或更新的方法可用。考慮所有編程語言。
// Set the date we're counting down to
var countDownDate = new Date("May 10, 2022 24:00:00").getTime();
// Update the count down every 1 second
var x = setInterval(function() {
// Get today's date and time
var now = new Date().getTime();
// Find the distance between now and the count down date
var distance = countDownDate - now;
// 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);
#demo {
font: normal 20px arial
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="demo"></div>
https://jsfiddle.net/r64zp93c/
uj5u.com熱心網友回復:
這似乎Date.UTC()是您正在尋找的東西。從檔案:
Date.UTC() 方法接受類似于
Date建構式的引數,但將它們視為 UTC。它回傳自 1970 年 1 月 1 日 00:00:00 UTC 以來的毫秒數。
所以而不是
var countDownDate = new Date("May 10, 2022 24:00:00").getTime();
你應該能夠做到
var countDownDate = Date.UTC(2022, 4, 10, 24, 0, 0);
(但也許將小時/天調整為 UTC 以匹配您需要的特定時間)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/471325.html
標籤:javascript html 日期 世界标准时间 倒数
