-1

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
j08691
  • 204,283
  • 31
  • 260
  • 272
Ayaan
  • 23
  • 6
  • Sounds like you need your date and time in a specific timezone, this related post may help: [get-date-time-for-a-specific-time-zone-using-javascript](https://stackoverflow.com/questions/11124322/get-date-time-for-a-specific-time-zone-using-javascript), here's one that is actually dealing with that exact timezone: [display-live-time-for-specific-time-zone-using-javascript](https://stackoverflow.com/questions/64184085/display-live-time-for-specific-time-zone-using-javascript) – Ryan Wilson Dec 07 '22 at 16:07

1 Answers1

2

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
phuzi
  • 12,078
  • 3
  • 26
  • 50