Solved

Adding seconds to a date

Posted on 2014-12-02
9
129 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

724 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