time intervals

M

Marco Alting

Can anyone tell me how to write a routine that uses time-intervals, I would
like to dynamically create an array of times like this:

9:00
9:30
10:00
10:30

This has an interval of half an hour, but I want to be flexible for future
use, so a variable called interval should be set to define the intervals.
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen in
news:comp.lang.javascript said:
Can anyone tell me how to write a routine that uses time-intervals, I would
like to dynamically create an array of times like this:

9:00
9:30
10:00
10:30

This has an interval of half an hour, but I want to be flexible for future
use, so a variable called interval should be set to define the intervals.


See reference in FAQ, and below.

Assuming that you do not need times between 23:59 and when-the-clocks-
go-back, you can use D = new Date("1/1/1 9:30") or with (D = new
Date()) setHours(9,30,0,0) or D = new Date(36000000-1800000) or
D = new Date(0,0,1,9,30,0) to initialise D ;
D.setMinutes(D.getMinutes()+30) to increment it; and pick out the
result with X = D.toGMTString().match(/\d+:\d\d/).

OTOH, you could do it as you would with pencil and paper. Start with
HH=9 and MM=30, then
MM += interval ; HH += Math.floor(MM/60) ; MM %= 60

Incrementing by fractional minutes should now be obvious.

D.setTime(D.getTime()+1800000) // alternative.

Then assign the resulting strings to successive array elements,
remembering to add some way of stopping.
 

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,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top