Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Javascript hours countdown getting wrong

My countdown shows the wrong hours with my current location. It shows 6 hours difference from my time zone. My time zone is Asia/Dhaka.

How do I get the correct hours? days, Minutes, and Seconds are correct. Only problem with hours.

// Got this function from thisinterestsme
function calculateChristmasCountdown() {

  //Get today's date.
  var now = new Date();

  //Get the current month. Add a +1 because
  //getMonth starts at 0 for January.
  var currentMonth = (now.getMonth() + 1);

  //Get the current day of the month.
  var currentDay = now.getDate();

  //Work out the year that the next Christmas
  //day will occur on.
  var nextChristmasYear = now.getFullYear();
  if (currentMonth == 12 && currentDay > 25) {
    //This year's Christmas Day has already passed.
    nextChristmasYear = nextChristmasYear + 1;
  }

  var nextChristmasDate = nextChristmasYear + '-12-25T00:00:00.000Z';
  var christmasDay = new Date(nextChristmasDate);

  //Get the difference in seconds between the two days.
  var diffSeconds = Math.floor((christmasDay.getTime() - now.getTime()) / 1000);

  var days = 0;
  var hours = 0;
  var minutes = 0;
  var seconds = 0;

  //Don't calculate the time left if it is Christmas day.
  if (currentMonth != 12 || (currentMonth == 12 && currentDay != 25)) {
    //Convert these seconds into days, hours, minutes, seconds.
    days = Math.floor(diffSeconds / (3600 * 24));
    diffSeconds -= days * 3600 * 24;
    hours = Math.floor(diffSeconds / 3600);
    diffSeconds -= hours * 3600;
    minutes = Math.floor(diffSeconds / 60);
    diffSeconds -= minutes * 60;
    seconds = diffSeconds;
  }

  //Add our counts to their corresponding HTML elements.
  document.getElementById('cws_xmas_days').innerHTML = days;
  document.getElementById('cws_xmas_hours').innerHTML = hours;
  document.getElementById('cws_xmas_minutes').innerHTML = minutes;
  document.getElementById('cws_xmas_seconds').innerHTML = seconds;
  setTimeout(calculateChristmasCountdown, 1000);
}
calculateChristmasCountdown();
<span id="cws_xmas_days"></span> days
<span id="cws_xmas_hours"></span> hours
<span id="cws_xmas_minutes"></span> minutes
<span id="cws_xmas_seconds"></span> seconds

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You have used the Z timezone specifier in the parsed date which means that you’re counting down to 25th December using the UTC/GMT timezone. Try removing the Z to get the local equivalent.

// Got this function from thisinterestsme
function calculateChristmasCountdown() {

  //Get today's date.
  var now = new Date();

  //Get the current month. Add a +1 because
  //getMonth starts at 0 for January.
  var currentMonth = (now.getMonth() + 1);

  //Get the current day of the month.
  var currentDay = now.getDate();

  //Work out the year that the next Christmas
  //day will occur on.
  var nextChristmasYear = now.getFullYear();
  if (currentMonth == 12 && currentDay > 25) {
    //This year's Christmas Day has already passed.
    nextChristmasYear = nextChristmasYear + 1;
  }

  var nextChristmasDate = nextChristmasYear + '-12-25T00:00:00.000';
  var christmasDay = new Date(nextChristmasDate);

  //Get the difference in seconds between the two days.
  var diffSeconds = Math.floor((christmasDay.getTime() - now.getTime()) / 1000);

  var days = 0;
  var hours = 0;
  var minutes = 0;
  var seconds = 0;

  //Don't calculate the time left if it is Christmas day.
  if (currentMonth != 12 || (currentMonth == 12 && currentDay != 25)) {
    //Convert these seconds into days, hours, minutes, seconds.
    days = Math.floor(diffSeconds / (3600 * 24));
    diffSeconds -= days * 3600 * 24;
    hours = Math.floor(diffSeconds / 3600);
    diffSeconds -= hours * 3600;
    minutes = Math.floor(diffSeconds / 60);
    diffSeconds -= minutes * 60;
    seconds = diffSeconds;
  }

  //Add our counts to their corresponding HTML elements.
  document.getElementById('cws_xmas_days').innerHTML = days;
  document.getElementById('cws_xmas_hours').innerHTML = hours;
  document.getElementById('cws_xmas_minutes').innerHTML = minutes;
  document.getElementById('cws_xmas_seconds').innerHTML = seconds;
  setTimeout(calculateChristmasCountdown, 1000);
}
calculateChristmasCountdown();
<span id="cws_xmas_days"></span> days
<span id="cws_xmas_hours"></span> hours
<span id="cws_xmas_minutes"></span> minutes
<span id="cws_xmas_seconds"></span> seconds
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading