0

I need to calculate a difference between two time using javascript: now is the moment when you load the page so I use:

now = new(Date).getMinutes()*60;

then is the first minute of the next hour. So for example now is 20:20 where I live: then will be 21:01. then my logic will be:

if(then-now>=120){
    diff=120;
}else{
    diff=then-now;
}

When I calculate the difference I need to include all the first minute of next hour. What can be the smartest way to calculate then in seconds?

Lelio Faieta
  • 6,457
  • 7
  • 40
  • 74
  • Not sure what your application is, but you will probably want to set a cookie too, so the user can't simply close the browser and reopen it, bypassing the 1 hour timer. – Gary Hayes May 16 '15 at 18:41
  • No. The app will prevent you to submit a form for last and first minute of each hour and it works. During this idle time it will show a countdown from 120 secs. But if I load the page at 59 minutes and 40 seconds countdown will start from 80 secs. 20 for last minute and 60 for first of next hour – Lelio Faieta May 16 '15 at 18:50
  • I see. The event is only allowed during those static times, not one hour increments since page first loaded. What sort of application is this? You're begging to have the system overloaded with users all trying to process forms at the same time rather than all throughout the hour. – Gary Hayes May 16 '15 at 19:08
  • This is not exactly an app. I have a close system with a dedicated server and few clients. The server casts messages to the clients using this page. The clients are not able to have any user interactions and are busy updating at hour change. So: I want to prevent users to cast messages while the client are busy (and will not display them) and my solution is to disable the form for last minute and first minute of next hour. Glad to hear any new idea! – Lelio Faieta May 16 '15 at 19:13
  • 1
    Ah... event is NOT allowed for those two minutes. Seems fine. Only other solution is to cache / queue the form submissions and automatically send behind the scenes when possible. But that seems like too much trouble. I think you're on the right path. Sorry for my curiosity. :D – Gary Hayes May 16 '15 at 19:21
  • you are welcome ;) in italy we say that two pairs of eyes are better than one :) – Lelio Faieta May 16 '15 at 19:34

2 Answers2

1

then = Date(year, month, day, new(Date).getHours()+1, 1, 0, 0); that gives you the date object, so just do then - now into seconds (getSeconds). You should watch for 25 hours in your +1 so you could actually use: then.setDate(someDate.getHour() + 1);

Also looks like your logic could just be min(diff, 120). Note that you may want to be more clear what unit 120 is in. Could even make a variable "twoMinutes=120" or "twoHours=120".

See: How to add number of days to today's date?

=== Update: Perhaps this code makes more sense

then = Date(year, month, day, new(Date).getHours(), 0, 0, 0);
then.setDate(then.getHour() + 1);
then.setDate(then.getMinute() +1);
YourLogic = min(then-now, 120)
Community
  • 1
  • 1
  • Thanks! I am not yet confident with dates in js. The goal is to have a countdown that starts from diff is diff is less then 120 seconds. For example if I load the page at 20.59.30 seconds I will have diff=90 seconds. If I load the page at 20.58.59 or earlier then 120 seconds – Lelio Faieta May 16 '15 at 18:46
0

This doesn't look very beautiful, but a very straight-forward way of calculating the 01 minute of the next hour is as follows:

new Date(new Date(new Date(new Date().getTime() + 3600000).setMinutes(1)).setSeconds(0))

This way you won't have to deal with problems like checking the hour 23 and fixing the date.

You can use this idea to calculate now the same way and check the number of seconds left till the above time:

var now = new Date().getTime();
var remainingSeconds = (new Date(new Date(new Date(now + 3600000).setMinutes(1)).setSeconds(0)) - now) / 1000;
downhand
  • 395
  • 1
  • 9
  • 23