<script>
function getRemainingTime() {
  // 发送 AJAX 请求
  let xhr = new XMLHttpRequest();
  xhr.onreadystatechange = function() {
    if (xhr.readyState === 4 && xhr.status === 200) {
      // 处理服务器返回的数据
      let remainingTime = xhr.responseText;
      
      // 将剩余时间转换为天、小时、分钟、秒数
      let days = Math.floor(remainingTime / (24 * 60 * 60));
      let hours = Math.floor((remainingTime % (24 * 60 * 60)) / (60 * 60));
      let minutes = Math.floor((remainingTime % (60 * 60)) / 60);
      let seconds = remainingTime % 60;
      
      // 将剩余时间输出到页面
      document.getElementById('remaining-days').innerHTML = days;
      document.getElementById('remaining-hours').innerHTML = hours;
      document.getElementById('remaining-minutes').innerHTML = minutes;
      document.getElementById('remaining-seconds').innerHTML = seconds;
    }
  };
  xhr.open('GET', 'time.php');
  xhr.send();
}
</script>
<p>剩余时间:</p>
<p><span id='remaining-days'></span>天<span id='remaining-hours'></span>小时<span id='remaining-minutes'></span>分钟<span id='remaining-seconds'></span>秒</p>
JavaScript 获取剩余时间并格式化显示 - 天、时、分、秒

原文地址: https://www.cveoy.top/t/topic/muQF 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录