Add time to Date object in javascript

I am trying to add time to a Date object in javascript, but not getting the expected results. I am trying to pull out a timer on a page and add it to the current time to get the unix timestamp value when the timer reaches zero. The time on the page is displayed as "HH: MM: SS". This is what I have:

time=getTimerText.split(":");
seconds=(time[0]*3600+time[1]*60+time[2])*1000;

to convert time to milliseconds.

fDate=new Date();
fDate.setTime(fDate.getTime()+seconds);

add milliseconds to javascript timestamp

alert(Math.round(fDate.getTime() / 1000));

convert javascript timestamp to unix timestamp

Since the timer is counting down, I have to get the same result every time I run the script, but I do not. Can anyone see what I can do wrong here?

+3
source share
2

, . , - explicity, time[2] :

seconds=(time[0]*3600+time[1]*60+(+time[2]))*1000;

(+time[2]) + .

+1

, 12 , , ( , ).

:

  • : "31/03/2013 12:00:00"
  • : fDate = 31/03/2013 00:00:00 GMT + 1
  • 12 , 0 fDate: fDate = 31/03/2013 13:00:00 GMT + 2

? 02:00 (AM) 31 ( ), 12 31- - 13:00 (1:00 PM).

, :

fDate.setTime(fDate.getTime() - fDate.getTimezoneOffset() * 60000);
fDate.setTime(fDate.getTime() + seconds);
fDate.setTime(fDate.getTime() + fDate.getTimezoneOffset() * 60000);
0

Source: https://habr.com/ru/post/1740227/


All Articles