This week's book giveaway is in the Java in General forum.
We're giving away four copies of Event Streams in Action and have Alexander Dean & Valentin Crettaz on-line!
See this thread for details.
Win a copy of Event Streams in Action this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

A question about a Real time minute counter implementation

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear all,

I have a requirement about implementing a counter of minutes starting from a specific date and I would appreciate if you could kindly give me a hand.

Problem description:
Let's say that we have a specific date, for example, 14th March 2008, that is, in JavaScript new Date(2008, 2, 14). What I need is to calculate the number of minutes between that date and today (today is obviously the current time at the moment where the HTML page including my JavaScript program is being opened in the web browser) and print the result as a real time clock, which indicates the number of minutes followed by the number of seconds which are being modified in real time each second.

Implementation:
I'm quite beginner in JavaScript and after looking W3School documentation and several examples, I've managed to implement the following code


You can save the above code in an html file and view the result.

I would like to ask you, kindly have a look at my code and tell me whether I'm on the right track. However, it seems to me that there is some bug in the code which unfortunately I couldn't manage to detect. For example if at the moment when you open the page, the value of the seconds (read from the current date) is 48 seconds (just an example), then after 59 (when it becomes 00) it does not increment the minutes variable in the code for the first time, but then for next iterations (I mean each updateCounter( ) function call every second), apparently it works as expected.

Thanks a lot for your attention and your guidance,
 
dariyoosh za
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I think I found the problem. Instead of using Math.round I had to use Math.floor in the getEllapsedMinutes function and now it works.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!