0

I have timer. Now the timer shows the remaining time until the end of the day by current time at PC, and then updated. But I need to bind the timer to the UTC +3 time zone.How can i do it right?

My code:

        function create_target_date() 
        {
            var target_date = new Date();
            //target_date.setDate(target_date.getDate()+1);
            target_date.setHours(23,59,59);
            return target_date;
        }

        function calculation_timer()
        {
            var target_date = create_target_date();
            var current_date = new Date();
            val_timer = target_date.getTime() - current_date.getTime();

            var hours = Math.floor(val_timer/1000/60/60);
            var minutes = Math.floor((val_timer-hours*60*60*1000)/1000/60);
            var seconds = Math.floor(((val_timer-hours*60*60*1000)-minutes*60*1000)/1000);

            document.getElementById('hours').innerHTML = hours;

            document.getElementById('minutes').innerHTML = minutes;

            document.getElementById('seconds').innerHTML = seconds;
        }

        function start_timer()
        {
            calculation_timer();
            id_timer = setInterval(calculation_timer,1000);
        }
aston_ua
  • 39
  • 6
  • 1
    Does this answer your question? [How to initialize a JavaScript Date to a particular time zone](https://stackoverflow.com/questions/15141762/how-to-initialize-a-javascript-date-to-a-particular-time-zone) – Heretic Monkey Mar 20 '20 at 21:37
  • no, it's not my answer – aston_ua Mar 20 '20 at 21:53

1 Answers1

0

Because

 var current_date = new Date();

is UTC time so in my function I use

var utcplus = 3;
....

  var hr = current_date.getHours() + utcplus;
 if (hr>=24) {hr=hr-24;}
if (hr<0) {hr=hr+24;}
var min = current_date.getMinutes();
var sec = current_date.getSeconds();

utcplus can be defined -12 to +12

Codebreaker007
  • 2,911
  • 1
  • 10
  • 22