LukeD1uk LukeD1uk - 1 year ago 90
Javascript Question

Beautifying a countup timer

I am trying to recreate a Javascript timer I saw on this website:

Specifically the

**"It has been 1 minute and 32 seconds"**

I have a normal counter but it displays like a digital clock, I've been googling for an hour now and have no luck finding a solution.

Here is a pen with the counter from that website, I was trying to learn from it and strip it down to just what I need but with no luck.

Help would be greatly appreciated.

Answer Source
   var sec =0, min=0, hour=0;
   if(sec==60){sec=0; min++;}
   if(min==60){min=0; hour++;}
   var str = 'it has been';
   str+= hour?' ' +hour + (hour==1?' hour':' hours'):'';
   str+= min?(hour?' and ':' ') +min+ (min==1?' minute':' minutes') :'';
   str+= sec?(min||hour?' and ':' ') +sec+ (sec==1?' second':' seconds'):'';

Here is the DEMO