Adding seconds to a date

I am trying to add seconds to a date but its not working properly.  I have a for loop that loops a simulated one hour which is 3600 seconds.  The problem is every time 60 seconds passes I want to add a minute to the data and console log the new date.

So for instance lets say the date is this
Tue Dec 2 07:01:00 2014

When Going thru the loop the time iteration would be like this
Tue Dec 2 07:02:00 2014
Tue Dec 2 07:03:00 2014
Tue Dec 2 07:04:00 2014
Tue Dec 2 07:05:00 2014
Tue Dec 2 07:06:00 2014
Tue Dec 2 07:07:00 2014
Tue Dec 2 07:08:00 2014
Tue Dec 2 07:09:00 2014
Tue Dec 2 07:10:00 2014
Tue Dec 2 07:11:00 2014
Tue Dec 2 07:12:00 2014

For a Hour the final date would be this
Tue Dec 2 08:00:00 2014


Remember all these days are in a unix timestamp

The code

var sixtySeconds=60;
var sixtyCount=0;
var t = new Date();
var date;

for (i = 0; i < 3600; i++) {
        
        

      if(sixtyCount==60){
       sixtySeconds=sixtySeconds+60;
       date=t.setSeconds(t.getSeconds() + sixtySeconds);
       sixtyCount=0;
      }
       else
        {
            sixtyCount=0; 
        }
}

Open in new window

stargateatlantisAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GaryCommented:
Javascript doesn't work on UNIX timestamp, it works on the client's OS datetime

But the problem is...?
0
stargateatlantisAuthor Commented:
The UNIX time isn't a issue the problem is adding the right seconds to the time  so that the output would be like this

Tue Dec 2 07:02:00 2014
Tue Dec 2 07:03:00 2014
Tue Dec 2 07:04:00 2014
Tue Dec 2 07:05:00 2014
Tue Dec 2 07:06:00 2014
Tue Dec 2 07:07:00 2014
Tue Dec 2 07:08:00 2014
Tue Dec 2 07:09:00 2014
Tue Dec 2 07:10:00 2014
Tue Dec 2 07:11:00 2014
Tue Dec 2 07:12:00 2014

For a Hour the final date would be this
Tue Dec 2 08:00:00 2014
0
GaryCommented:
So you basically just want to display a minute by minute countdown?
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

stargateatlantisAuthor Commented:
It would get the current time which for example would be

Start date time
Tue Dec 2 07:02:00 2014

The final time and date would be
Tue Dec 2 08:00:00 2014

In between it would log all the new time value as well.  So every minute the time gets a console.log
0
GaryCommented:
Okey dokey, pondering the best way to do this.
0
Brian TaoSenior Business Solutions ConsultantCommented:
Try this:
var sixtySeconds=60;
var sixtyCount=0;
var t = new Date();
var date;

for (i = 0; i < 3600; i++) {
    if(sixtyCount == 60){
        date=new Date(t.setSeconds(t.getSeconds() + sixtySeconds));
        console.log(date.toLocaleString());
        sixtyCount=0;
    }else{
        sixtyCount++; 
    }
}

Open in new window


See http://jsfiddle.net/8b42r7ru/
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Albert Van HalenAnalyst developerCommented:
Formatting is a different issue. There are plenty of libraries that takes care of formatting a date object. For example datejs or momentjs or this fiddle here (http://jsfiddle.net/phZr7/1/)

For adding seconds to a certain date I prototyped the date object with 3 methods.
Date.prototype.addHours = function(hrs) {
	this.setHours(this.getHours() + hrs);
	return this;
}

Date.prototype.addMinutes = function(mnts) {
	this.setMinutes(this.getMinutes() + mnts);
	return this;
}

Date.prototype.addSeconds = function(scnds) {
	this.setSeconds(this.getSeconds() + scnds);
	return this;
}

Open in new window

Check out this fiddle : http://jsfiddle.net/c1f6fpgr/

Instead of adding 3600 seconds to a certain date you can add 60 minutes or 1 hour just by calling the appropriate method.

It's easy to extend the date object with new methods like addDays, addMonths etc.
0
GaryCommented:
How's this work for you
<script>
var weekday = new Array(7);
weekday[0]=  "Sun";
weekday[1] = "Mon";
weekday[2] = "Tue";
weekday[3] ="Wed";
weekday[4] = "Thu";
weekday[5]= "Fri";
weekday[6] = "Sat";
 
var month = new Array();
month[0] = "Jan";
month[1]= "Feb";
month[2] = "Mar";
month[3] ="Apr";
month[4] = "May";
month[5] = "Jun";
month[6] = "Jul";
month[7]= "Aug";
month[8] = "Sep";
month[9] = "Oct";
month[10] ="Nov";
month[11] = "Dec";
 
d = new Date();
var next_hour = d.getHours()+1; // up to the next hour

endtime = d.setHours(d.getHours()+1);
console.log(weekday[d.getDay()]+" "+month[d.getDay()]+" "+d.getUTCDate()+" "+d.getUTCHours()+":"+d.getUTCHours()+":00"+d.getFullYear())

counter = setInterval(function () { 
	d=new Date()
	console.log(weekday[d.getDay()]+" "+month[d.getDay()]+" "+d.getUTCDate()+" "+d.getUTCHours()+":"+d.getUTCHours()+":00"+d.getFullYear())
	if(next_hour>=d.getHours()){
		window.clearInterval(counter)
	}
}, 60000);
</script>

Open in new window

0
GaryCommented:
Are you wanting this in real time or just a simulation?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.