Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 155
  • Last Modified:

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

0
stargateatlantis
Asked:
stargateatlantis
3 Solutions
 
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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
 
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now