• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 193
  • 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
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

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