By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,382 Members | 1,631 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,382 IT Pros & Developers. It's quick & easy.

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

P: n/a
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'?


Jul 20 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"FN" <ne**************@DELETECAPSyahoo.com> writes:
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2

P: n/a
JRS: In article <d6**********@hotpop.com>, seen in
news:comp.lang.javascript, Lasse Reichstein Nielsen <lr*@hotpop.com>
posted at Fri, 11 Jul 2003 02:11:40 :-
"FN" <ne**************@DELETECAPSyahoo.com> writes:
I'm new to javascript and internet research is turning up weird things. I
just want to add 5 seconds to a date value.
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.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME.
Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.
Jul 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.