Solved

Adding seconds to a date

Posted on 2014-12-02
9
119 Views
Last Modified: 2014-12-03
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
Comment
Question by:stargateatlantis
9 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 40477663
Javascript doesn't work on UNIX timestamp, it works on the client's OS datetime

But the problem is...?
0
 

Author Comment

by:stargateatlantis
ID: 40477673
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
 
LVL 58

Expert Comment

by:Gary
ID: 40477677
So you basically just want to display a minute by minute countdown?
0
 

Author Comment

by:stargateatlantis
ID: 40477682
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 58

Expert Comment

by:Gary
ID: 40477704
Okey dokey, pondering the best way to do this.
0
 
LVL 9

Accepted Solution

by:
Brian Tao earned 167 total points
ID: 40477794
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
 
LVL 19

Assisted Solution

by:Albert Van Halen
Albert Van Halen earned 167 total points
ID: 40477980
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
 
LVL 58

Assisted Solution

by:Gary
Gary earned 166 total points
ID: 40478725
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
 
LVL 58

Expert Comment

by:Gary
ID: 40478833
Are you wanting this in real time or just a simulation?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now