Hello! Tell me how to rearrange this timer at the time of UTC msk and not the current one on the computer.

var finaltime = Math.floor((new Date(2016,5,28,24,0,0)).getTime()/1000); var timerId = window.setInterval( display, 100); var el = document.getElementById("seconds"); var date = new Date; function display() { var seconds = Math.floor((finaltime - (new Date()).getTime()/1000)) ,minutes ,hours ,days ; //date.getUTCFullYear(),date.getUTCMonth(),date.getUTCDate(),date.getUTCHours(),date.getUTCMinutes(),date.getUTCSeconds() if (seconds <= 0) { window.clearInterval( timerId); $.ajax({ type: 'POST', url: 'response.php?', data: 'name=Andrew', success: function(data) { $('#show').html(data); } }); return; } days = Math.floor(seconds/86400); seconds = seconds % 86400; hours = zeropad( Math.floor( seconds / 3600)); seconds = seconds % 3600; minutes = zeropad( Math.floor( seconds / 60)); seconds = zeropad( seconds % 60); el.innerHTML = days + ' дней ' + hours + ":" + minutes + ":" + seconds; } function zeropad(n) { return n < 10 ? '0'+n : n.toString(); } 
 <b id="seconds">0</b> <div id="show"></div> 

  • one
    UTC msk ? What is it like? - Grundy
  • To take UTC and +3 - Bogdan
  • use only utc date everywhere and there will be no problem - Grundy
  • I do not really understand js, could you show a sample code. Tried to do so var seconds = Math.floor((finaltime - (new Date(date.getUTCFullYear(),date.getUTCMonth(),date.getUTCDate(),date.getUTCHours(),date.getUTCMinutes(),date.getUTCSeconds())).getTime()/1000)) but the timer just stands. - Bogdan
  • ten times a second - not often? What do you have on the server side? - Igor

1 answer 1

The date in javascript is stored in UTC, so it is sufficient to create an end date, such as UTC time , corresponding to Moscow time, i.e.

MSC time: 2016-06-28 21:00:00
UTC time: 2016-06-28 18:00:00 (MSK-3 hours)

And then everything remains as it is:

 var finaltime = Math.floor((Date.UTC(2116, 5, 28, 21, 0, 0)) / 1000); var timerId = window.setInterval(display, 500); var el = document.getElementById("seconds"); function display() { var seconds = Math.floor((finaltime - (new Date()).getTime() / 1000)), minutes, hours, days; if (seconds <= 0) { window.clearInterval(timerId); console.log('Done'); return; } days = Math.floor(seconds / 86400); seconds = seconds % 86400; hours = zeropad(Math.floor(seconds / 3600)); seconds = seconds % 3600; minutes = zeropad(Math.floor(seconds / 60)); seconds = zeropad(seconds % 60); el.innerHTML = days + ' дней ' + hours + ":" + minutes + ":" + seconds; } function zeropad(n) { return n < 10 ? '0' + n : n.toString(); } 
 <b id="seconds">0</b> <div id="show"></div> 

  • Thank. I needed it. I apologize to whom I confused) - Bogdan
  • Kaef, now I will often look at these remaining days and cry, because I don’t live to 2116 - Mr. Black
  • @Doofy, you are too pessimistic :) - Grundy