Convert from VBScript...

D

David

Hi,

I have a script in one of my .asp pages which I think is written in
VBScript (I did not write it). I would like to know how to do the
following in Javascript.

Have a combo on my page which basically lists all the following
Tusdays & Thursdays dates, up to a set qty, i.e. 20 in the list.

E.g. todays date is 2nd August, so the combo would list 20 lines of:

Tusday 3rd August
Thursday 5th August
Tusday 10th August
Thursday 12 August

and so on for another 16 lines...

How is this possible ?

__________________________________________

The code I have at the mo has been tweeked by myself, but I can only
get the combo to list the Tuesdays first, then all the Thursdays, not
all in date order.

________________

<SCRIPT language='vbscript'>
Sub Window_Onload
Dim TheDate
Dim Count
Dim Options
TheDate = Date + vbTuesday - WeekDay(Date)
TheDate2 = Date + vbThursday - WeekDay(Date)


If TheDate < Date Then TheDate = TheDate + 7
Set Options = Document.All.Date.Options
For Count = 1 To 20
StrDate = "Tuesday " & Right("0" & Day(TheDate),2) & "/" &
Right("0" &_
Month(TheDate),2) & "/" & Year(TheDate)
Options.Add Window.Option(StrDate,"for " & StrDate)
TheDate = TheDate + 7
Next

If TheDate2 < Date Then TheDate2 = TheDate2 + 7
Set Options = Document.All.Date.Options
For Count = 1 To 20
StrDate2 = "Thursday " & Right("0" & Day(TheDate2),2) & "/" &
Right("0" &_
Month(TheDate2),2) & "/" & Year(TheDate2)
Options.Add Window.Option(StrDate2,"for " & StrDate2)
TheDate2 = TheDate2 + 7
Next


End Sub
</script>

_______________________________

Hope you can help ?
David
 
K

kaeli

david@scene- said:
Hi,

I have a script in one of my .asp pages which I think is written in
VBScript (I did not write it). I would like to know how to do the
following in Javascript.

Have a combo on my page which basically lists all the following
Tusdays & Thursdays dates, up to a set qty, i.e. 20 in the list.
<snip>

The code is also IE only.
The code I have at the mo has been tweeked by myself, but I can only
get the combo to list the Tuesdays first, then all the Thursdays, not
all in date order.

I checked this in IE. Should work in Netscape, too. Check other browsers as
needed. Watch for word-wrap.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
"http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>
<title> New Document </title>
<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.getMonth()+1;
tmp = tmp<10?"0"+tmp:tmp;
r_date = tmp+"/";
tmp = t_date.getDate();
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["f1"].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() != 2 && theDate.getDay() != 4)
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>
</head>

<body onLoad = "fillIt()">
<form name="f1" id="f1">
<select name="date" id="date"></select>
</form>
</body>
</html>

--
 
B

ben

david, try something like:


<script>

function FillThemDateOptions()
{
var dateSelector = document.form2.JSDate;
var maxOptions = 20;
var today = new Date();

for(var i=0;i<((maxOptions*7)/2);i+=7)
{
var tueDate =
new Date(today.getYear(), today.getMonth(),
today.getDate()+i+1);
var thuDate =
new Date(today.getYear(), today.getMonth(),
today.getDate()+i+3);

dateSelector.options.add(FormatTheDateAsASelectOptionThing(tueDate));
dateSelector.options.add(FormatTheDateAsASelectOptionThing(thuDate));
}
}

function FormatTheDateAsASelectOptionThing(d)
{
var aWeekDayNames =
new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');

var sText =
aWeekDayNames[d.getDay()] +
' ' +
((d.getDate()<10)?'0':'') +
d.getDate() +
'/' +
((d.getMonth()<10)?'0':'') +
d.getMonth() +
'/' +
d.getFullYear();

var sValue = 'for ' + sText;

return new Option(sText, sValue);
}

window.onload = FillThemDateOptions;

</script>

irt.org have a load of FAQs on javascript:
http://developer.irt.org/script/date.htm give them a try for some
ideas.

ben
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated
Mon, 2 Aug 2004 11:32:31, seen in kaeli
You've not specified what happens if today is Tue or Thu, unless we take
"following" literally.

If the number is always to be even, one can take advantage of the fact
that the duration will always be a little over 10 weeks.

I checked this in IE. Should work in Netscape, too. Check other browsers as
needed. Watch for word-wrap.

But did you check it in February or September at sensitive times of day?
Ten weeks from now it is still Summer.

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


There are circumstances in which that can be correct; except that
there's no point in typing 24*60*60*1000 when 864e5 will suffice. That
code may be one of them; but if a Sunday were wanted a special one would
ISTM be missed or duplicated.


function fillIt()
var theDate = new Date();
theDate = addDays(theDate,1);


In late Winter, if the code is started late enough in the day, a day
will be missed; and vice versa for early Summer morning.

Function addDays could be

function addDays(t_date, days) { // but alters t_date
t_date.setDate(t_date.getDate() + days ) }

Alternatively, use setHours(12) early on.




The following will write the desired dates :-


D = new Date() ; K = 20 ;
while (K) {
D.setDate(D.getDate()+1) // AddADay
X = D.getDay()
if ( X==2 || X==4 ) document.write(D, ' ', K--, '<br>')
}



If speed matters, however, find today's day-of-week, calculate the
offset to the Tue/Thu before the next, add it, then add 2 & 5
appropriately 20 times.



Otherwise, if the dates are to be used in ISO-8601 form such as
"2004-08-02 Mon" then one can stick first the Tuesdays then the
Thursdays in an array, and sort it.
 
D

David Gordon

Ben,

Thanks for that, but I cannot get your script working with a form....

</script>


</head>

<body onLoad = "FillThemDateOptions()">
<form name="form2" id="form2">
<select name="JSDate" id="JSDate"></select>
</form>
</body>
</html>


I know this simple, but I must be missing a trick here ?
 
D

David Gordon

Hi,

Just to let you know that I suddenly had a brainwave with my original
code and changed it to:

<SCRIPT language='vbscript'>
Sub Window_Onload
Dim TheDate
Dim Count
Dim Options
TheDate = Date + vbTuesday - WeekDay(Date)
TheDate2 = Date + vbThursday - WeekDay(Date)


If TheDate < Date Then TheDate = TheDate + 7
Set Options = Document.All.Date.Options
For Count = 1 To 20


StrDate = "Tuesday " & Right("0" & Day(TheDate),2) & "/" &
Right("0" &_
Month(TheDate),2) & "/" & Year(TheDate)
Options.Add Window.Option(StrDate,"for " & StrDate)
TheDate = TheDate + 7


StrDate2 = "Thursday " & Right("0" & Day(TheDate2),2) & "/" & Right("0"
&_
Month(TheDate2),2) & "/" & Year(TheDate2)
Options.Add Window.Option(StrDate2,"for " & StrDate2)
TheDate2 = TheDate2 + 7


Next

End Sub
</script>

Thanks again for all your help and code. I'm testing your different
versions now.

David.
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated
Tue, 3 Aug 2004 15:45:00, seen in David
Gordon said:
Hi,

Just to let you know that I suddenly had a brainwave with my original
code and changed it to:

<SCRIPT language='vbscript'>
Sub Window_Onload
Dim TheDate
Dim Count
Dim Options
TheDate = Date + vbTuesday - WeekDay(Date)
TheDate2 = Date + vbThursday - WeekDay(Date)

In principle, one should call only one of Date, Time, Now on any given
page (unless one wishes to show the passage of time), since they are not
constant functions. In this case, that's unlikely to matter.
If TheDate < Date Then TheDate = TheDate + 7

If TheDate2 < Date ... ' also?

Alternatively, Date + (7 + vbTuesday - WeekDay(Date)) mod 7
or Date + (6 + vbTuesday - WeekDay(Date)) mod 7 + 1
Set Options = Document.All.Date.Options
For Count = 1 To 20

Twice as many as I thought you wanted.
StrDate = "Tuesday " & Right("0" & Day(TheDate),2) & "/" &

or Right(100 + Day(TheDate), 2) . IMHO,
that should be made a function, for legibility and modularity.

And how do you propose to transfer that reward to those who answer your
questions? <URL:http://www.merlyn.demon.co.uk/index.htm#Spon>.
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top