Saturdays

M

Maarten

Hi,

i need to fill a dropdownlist in a frame with only saturdays.

The code must be like:

<select class="fld" size="1" name="arrivaldate">
<option value="17/01/2004">17/01/2004</option>
<option value="24/01/2004">17/01/2004</option>
<option value="31/01/2004">17/01/2004</option>
</select>

and so on....

Can somebody show how to do this with JS?
Thx
Maarten.
 
L

Lee

Maarten said:
Hi,

i need to fill a dropdownlist in a frame with only saturdays.

The code must be like:

<select class="fld" size="1" name="arrivaldate">
<option value="17/01/2004">17/01/2004</option>
<option value="24/01/2004">17/01/2004</option>
<option value="31/01/2004">17/01/2004</option>
</select>

and so on....

Can somebody show how to do this with JS?

<html>
<head>
<script type="text/javascript">
// function zp zero pads a number to 2 digits.
function zp(n){return (n<10)?"0"+n:n}
var saturdays=new Array();
var now=new Date();
var nextYear=new Date(now.getFullYear()+1,0,1);
// nextYear is the first day of next year
var saturday=new Date(now.setDate(now.getDate()+6-now.getDay()));
// saturday is now the first Saturday after yesterday.
while(saturday<nextYear){
var DDMMYYYY=zp(saturday.getDate())+"/"+
zp(saturday.getMonth()+1)+"/"+
saturday.getFullYear();
saturdays[saturdays.length]="<option value=\""+
DDMMYYYY+"\">"+
DDMMYYYY+"</option>";
saturday.setDate(saturday.getDate()+7);
}
</script>
</head>
<body>
<form>
<select>
<script type="text/javascript">
document.write(saturdays.join(""));
</script>
</select>
</form>
</body>
</html>
 
B

Brian Genisio

Maarten said:
Hi,

i need to fill a dropdownlist in a frame with only saturdays.

The code must be like:

<select class="fld" size="1" name="arrivaldate">
<option value="17/01/2004">17/01/2004</option>
<option value="24/01/2004">17/01/2004</option>
<option value="31/01/2004">17/01/2004</option>
</select>

and so on....

Can somebody show how to do this with JS?
Thx
Maarten.

Here is a quick way to do it... I am sure there are others...

var msInDay = 1000 * 60 * 60 * 24;

// Get today's date
var today = new Date();

// Set the date to saturday 0 = Sunday to 6 = Saturday
var dayOffset = (6 - today.getDay()) * msInDay;

var saturday = new Date();

// output the next 12 saturdays
for(i=0; i<12; i++)
{
saturday.setTime( today.getTime() + dayOffset + (7 * i * msInDay) );
document.write(saturday.toDateString() + "<BR>");
}
 
L

Lasse Reichstein Nielsen

Brian Genisio said:
Here is a quick way to do it... I am sure there are others...

var msInDay = 1000 * 60 * 60 * 24;

Bad idea. The date object has much safer ways to add one day, that
won't get confuzed by changes to and from daylight saving time.
// Get today's date
var today = new Date();

// Set the date to saturday 0 = Sunday to 6 = Saturday
var dayOffset = (6 - today.getDay()) * msInDay;

Just use
var dayOffset = 6 - today.getDay();
and use it on the date, not the milliseconds.

Shorter version:

var date = new Date();
date.setDate(date.getDate()+6-date.getDay()); // first coming Saturday.
for(var i=0;i<10;i++) {
document.write(date.toString() + "<br>");
date.setDate(date.getDate()+7);
}

/L
 
J

Janwillem Borleffs

Maarten said:
i need to fill a dropdownlist in a frame with only saturdays.

The code must be like:

<select class="fld" size="1" name="arrivaldate">
<option value="17/01/2004">17/01/2004</option>

<script type="text/javascript">
var date, y = 2004; d = new Date(y,0,1);

for (;;) {
d.setDate(d.getDate() + ( 6 - d.getDay() ) );
if (d.getFullYear() != y) break;

date = ('00' + d.getDate()).slice(-2) + '/' +
('00' + (d.getMonth() + 1)).slice(-2) + '/' +
d.getFullYear();

document.writeln(
'<option value="', date, '">', date, '</option>'
);
d.setDate(d.getDate() + 1);
}
</script>


HTH;
JW
 

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

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top