Combo with dates

D

David

Hi,

I have a code for filling a dropdown, but I cannot work out how to make
it fill with every date, including todays date, as required below

i.e.
05/10/2006
06/10/2006
07/10/2006
08/10/2006
09/10/2006
10/10/2006
11/10/2006
12/10/2006
13/10/2006
14/10/2006


It needs to display about 10 dates

------------------------------------

The code I have is:

<script type="text/javascript" language="javascript">
function formatDate(t_date, format)
{
/* t_date is expected to be a Date() and format is a string */

var r_date = null; /* string to return */

/* add more formats as desired */
switch (format)
{
case ("mm/dd/yyyy"):


tmp = t_date.getDate();
tmp = tmp<10?"0"+tmp:tmp;


r_date = tmp+"/";


tmp = t_date.getMonth()+1;
tmp = tmp<10?"0"+tmp:tmp;





r_date += tmp+"/"+t_date.getFullYear();
break;
default:
alert("That is not a valid format");
}
return r_date;
}

function addDays(t_date,days)
{
return new Date(t_date.getTime() + days*24*60*60*1000);
}

function fillIt()
{
var s = document.forms["formname"].elements["Date"];
var o;
var theDate = new Date();
var i;

for (i=0; i<20; i++)
{
/* if this is a Tuesday (2) or Thursday (4), put in option, else
add a
day and check again */
while (theDate.getDay() != 0 )
theDate = addDays(theDate, 1);
/* we get here, it's either Tursday or Thursday - write option,
increment, and loop */
tmp = formatDate(theDate, "mm/dd/yyyy");
o = new Option(tmp, tmp);
s.options = o;
theDate = addDays(theDate,1);
}
}
</script>


-----------------------------------


Appreciate your help, thanks :)

David
 
E

Evertjan.

David wrote on 05 okt 2006 in comp.lang.javascript:
Hi,

I have a code for filling a dropdown, but I cannot work out how to make
it fill with every date, including todays date, as required below

i.e.
05/10/2006
06/10/2006
07/10/2006
08/10/2006
09/10/2006
10/10/2006
11/10/2006
12/10/2006
13/10/2006
14/10/2006


It needs to display about 10 dates

=================================================
<script type='text/javascript'>

function nextday(x){
return new Date(x.getYear(), x.getMonth(), x.getDate()+1);
};

function DDMMYYYY(x){
return T(x.getDate())+'/'+T(x.getMonth()+1)+'/'+x.getYear();
};

function T(x){
return (x<10)?'0'+x:x;
};

var d = new Date();
var t = '';

for (var i=0;i<10;i++) {
t += '<option>'+DDMMYYYY(d)+'</option>';
d = nextday(d);
}

document.write('<SELECT>'+t+'</SELECT>');

</script>
=======================================

if you want only Tuesdays (2) or Thursdays (4):

=======================================

......
for (var i=0;i<10;i++) {
while (d.getDay() != 2 && d.getDay() != 4 )
d = nextday(d);
t += '<option>'+DDMMYYYY(d)+'</option>';
d = nextday(d);
}
.....

=======================================
 
R

RobG

David said:
Hi,

I have a code for filling a dropdown, but I cannot work out how to make
it fill with every date, including todays date, as required below

i.e.
05/10/2006
06/10/2006
07/10/2006
08/10/2006
09/10/2006
10/10/2006
11/10/2006
12/10/2006
13/10/2006
14/10/2006

Your codes seems to want only Tuesdays and Thursdays rather than every
day.

It needs to display about 10 dates

------------------------------------

The code I have is:

<script type="text/javascript" language="javascript">

The language attribute is deprecated so remove it, keep type.

function formatDate(t_date, format)
{
/* t_date is expected to be a Date() and format is a string */

If you want t_date to be today, then start with:

var t_date = new Date();
var r_date = null; /* string to return */

Initialising r_date with the value null serves no useful purpose in
this instance.

/* add more formats as desired */
switch (format)
{
case ("mm/dd/yyyy"):


tmp = t_date.getDate();
tmp = tmp<10?"0"+tmp:tmp;

It is probably better to have a small function that adds the leading
zero rather than repeating the code, say:

function addZ(num){ return (num < 10)? '0' + num : '' + num; }

Then:

tmp = addZ(t_date.getDate());
r_date = tmp+"/";

tmp = t_date.getMonth()+1;
tmp = tmp<10?"0"+tmp:tmp;

r_date += tmp+"/"+t_date.getFullYear();
break;

Instead of all that, why not:

tmp = addZ(t_date.getDate()) + '/'
+ addZ(t_date.getMonth()+1) + '/'
+ t_date.getFullYear();

default:
alert("That is not a valid format");
}
return r_date;
}

function addDays(t_date,days)
{
return new Date(t_date.getTime() + days*24*60*60*1000);

Adding 24hours worth of milliseconds is risky in places where daylight
saving is in effect, you will skip a day when going into daylight
saving and count a day twice on the way out. Just add the required
number of days to the date and avoid the issue:

return t_date.setDate(t_date.getDate() + (+days));

The brackets around +days is not essential but may help maintenance.
Note that adding days beyond the end of the month is OK: 30-Jan + 3
days gives 02-Feb.


Try this version[1] as an example:

function addDates(f){
var numOptions = 10;
var d = new Date(f.startDate.value);
var sel = f.date;
sel.length = 0; // Removes existing options
var dateString, dayNum;

while (numOptions) {
dayNum = d.getDay();
if (2 == dayNum || 4 == dayNum){
dateString = addZ(d.getDate()) + '/'
+ addZ(d.getMonth() + 1) + '/'
+ d.getFullYear();
sel.options[sel.options.length] = new Option(dateString);
--numOptions;
}
d.setDate(d.getDate() + 1);
}
}

function addZ(num){
return (num < 10)? '0' + num : '' + num;
}

</script>

<form action="">
<input type="text" name="startDate" value="2006/09/25">
<select name="date">
<option>Click "Add dates" to add dates
</select>
<input type="button" value="Add dates"
onclick="addDates(this.form)">
</form>


1. That the input date should be validated before trying to use it,
this version returns only Tuesdays and Thursdays in the option list
 
D

David

RobG said:
David said:
Hi,

I have a code for filling a dropdown, but I cannot work out how to make
it fill with every date, including todays date, as required below

i.e.
05/10/2006
06/10/2006
07/10/2006
08/10/2006
09/10/2006
10/10/2006
11/10/2006
12/10/2006
13/10/2006
14/10/2006

Your codes seems to want only Tuesdays and Thursdays rather than every
day.

It needs to display about 10 dates

------------------------------------

The code I have is:

<script type="text/javascript" language="javascript">

The language attribute is deprecated so remove it, keep type.

function formatDate(t_date, format)
{
/* t_date is expected to be a Date() and format is a string */

If you want t_date to be today, then start with:

var t_date = new Date();
var r_date = null; /* string to return */

Initialising r_date with the value null serves no useful purpose in
this instance.

/* add more formats as desired */
switch (format)
{
case ("mm/dd/yyyy"):


tmp = t_date.getDate();
tmp = tmp<10?"0"+tmp:tmp;

It is probably better to have a small function that adds the leading
zero rather than repeating the code, say:

function addZ(num){ return (num < 10)? '0' + num : '' + num; }

Then:

tmp = addZ(t_date.getDate());
r_date = tmp+"/";

tmp = t_date.getMonth()+1;
tmp = tmp<10?"0"+tmp:tmp;

r_date += tmp+"/"+t_date.getFullYear();
break;

Instead of all that, why not:

tmp = addZ(t_date.getDate()) + '/'
+ addZ(t_date.getMonth()+1) + '/'
+ t_date.getFullYear();

default:
alert("That is not a valid format");
}
return r_date;
}

function addDays(t_date,days)
{
return new Date(t_date.getTime() + days*24*60*60*1000);

Adding 24hours worth of milliseconds is risky in places where daylight
saving is in effect, you will skip a day when going into daylight
saving and count a day twice on the way out. Just add the required
number of days to the date and avoid the issue:

return t_date.setDate(t_date.getDate() + (+days));

The brackets around +days is not essential but may help maintenance.
Note that adding days beyond the end of the month is OK: 30-Jan + 3
days gives 02-Feb.


Try this version[1] as an example:

function addDates(f){
var numOptions = 10;
var d = new Date(f.startDate.value);
var sel = f.date;
sel.length = 0; // Removes existing options
var dateString, dayNum;

while (numOptions) {
dayNum = d.getDay();
if (2 == dayNum || 4 == dayNum){
dateString = addZ(d.getDate()) + '/'
+ addZ(d.getMonth() + 1) + '/'
+ d.getFullYear();
sel.options[sel.options.length] = new Option(dateString);
--numOptions;
}
d.setDate(d.getDate() + 1);
}
}

function addZ(num){
return (num < 10)? '0' + num : '' + num;
}

</script>

<form action="">
<input type="text" name="startDate" value="2006/09/25">
<select name="date">
<option>Click "Add dates" to add dates
</select>
<input type="button" value="Add dates"
onclick="addDates(this.form)">
</form>


1. That the input date should be validated before trying to use it,
this version returns only Tuesdays and Thursdays in the option list

----------------------

Thanks Rob,

How do I return all days including today ?

David
 

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,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top