چگونه با html یه تایمر با زمان خاص بسازیم؟

ali_e110

تازه وارد
سلام اساتید عزیز اگه بخوایم یه تایمر با زمان خاص و رو به گذر داشته باشیم باید چیکار کرد ؟ مثلا من یه گروه موسقی دارم که سال 90 راه افتاده حالا میخوام برا این گروه یه سایت بزنم با یه صفحه html ساده که این گروه از روز تاسیسش مثلا 7 سال و 6 ماه و 10 روز و 5 ساعت و 10 دقیقه و 30 ثانیه میگذره و همینجوری ادامه پیدا کنه طبق ساعت و روز و ماه و سال و تاریخ تا آخر کسی هست کمکم کنه؟

 

amin.hajihasani

تازه وارد
سلام اصولا نوشتن کانتر رو به بالا یا پایین راحته از کد زیر استفاده بکن:

JavaScript:
<script>
// Set the date we're counting up to
var countupDate = new Date("Jan 5, 2007 15:37:25").getTime();

// Update the count up 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 up date
  var distance = now - countupDate;

  // 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);

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

  // If the count up is finished, write some text
  if (distance < 0) {
    clearInterval(x);
    document.getElementById("demo").innerHTML = "EXPIRED";
  }
}, 1000);
</script>
اما اگر میخوای رو به پایین بره از کد زیر استفاده کن:
JavaScript:
<!-- Display the countdown timer in an element -->
<p id="demo"></p>

<script>
// Set the date we're counting down to
var countDownDate = new Date("Jan 5, 2021 15:37:25").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);

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

  // If the count down is finished, write some text
  if (distance < 0) {
    clearInterval(x);
    document.getElementById("demo").innerHTML = "EXPIRED";
  }
}, 1000);
</script>
 
  • Like
واکنش‌ها[ی پسندها]: tpark
بالا