easy for you! - how do I add 5 seconds to date?

F

FN

I'm new to javascript and internet research is turning up weird things. I
just want to add 5 seconds to a date value.

this = new date() // I believe this sets the time, too, right?

//now how do I add 5 seconds or even 5000 seconds to the variable 'this'?
 
L

Lasse Reichstein Nielsen

FN said:
I'm new to javascript and internet research is turning up weird things. I
just want to add 5 seconds to a date value.

this = new date() // I believe this sets the time, too, right?

Syntax error. The word "this" is a keyword and cannot be used as a variable
name. Also, the date constructor is called "Date" with a capital "D".

So, let's say you have:

var now = new Date();
//now how do I add 5 seconds or even 5000 seconds to the variable 'this'?

now.setSeconds(now.getSeconds() + 5);

For anything you'll ever want to know about dates in Javascript, and
then some, check: <URL:http://www.merlyn.demon.co.uk/js-dates.htm>.

/L
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen in
now.setSeconds(now.getSeconds() + 5);

Or
now.setTime(now.getTime() + 5000)

I would use the former if thinking of clock time, and the latter if
thinking of scientific time; but the two will be fully interchangeable,
at least until the software understands Leap Seconds (and that will
destroy many otherwise good algorithms).


There is one minor trap.

If I add 5 seconds to the value of a Date Object, it then represents a
moment undoubtedly 5 seconds later.

But the following code

now = new Date('2003/03/30 01:59:59')
S = now.toLocaleString()
now.setSeconds(now.getSeconds() + 5);
S += ' # ' + now.toLocaleString()

gives me

03/30/2003 01:59:59 # 03/30/2003 01:00:04

which illustrates no fewer than two errors and a possible surprise.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top