Convert from VBScript...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David

    Convert from VBScript...

    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='vbscr ipt'>
    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.Da te.Options
    For Count = 1 To 20
    StrDate = "Tuesday " & Right("0" & Day(TheDate),2) & "/" &
    Right("0" &_
    Month(TheDate), 2) & "/" & Year(TheDate)
    Options.Add Window.Option(S trDate,"for " & StrDate)
    TheDate = TheDate + 7
    Next

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


    End Sub
    </script>

    _______________ _______________ _

    Hope you can help ?
    David
  • kaeli

    #2
    Re: Convert from VBScript...

    In article <c178e3e8.04080 20656.51cb9aa3@ posting.google. com>, david@scene-
    double.co.uk enlightened us with...[color=blue]
    > 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.
    >[/color]
    <snip>

    The code is also IE only.
    [color=blue]
    >
    > 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.
    >[/color]

    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="javas cript">
    function formatDate(t_da te, 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.getFul lYear();
    break;
    default:
    alert("That is not a valid format");
    }
    return r_date;
    }

    function addDays(t_date, days)
    {
    return new Date(t_date.get Time() + days*24*60*60*1 000);
    }

    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(theD ate, "mm/dd/yyyy");
    o = new Option(tmp, tmp);
    s.options[i] = o;
    theDate = addDays(theDate ,1);
    }
    }
    </script>
    </head>

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

    --
    --
    ~kaeli~
    When you choke a smurf, what color does it turn?



    Comment

    • ben

      #3
      Re: Convert from VBScript...

      david, try something like:


      <script>

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

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

      dateSelector.op tions.add(Forma tTheDateAsASele ctOptionThing(t ueDate));
      dateSelector.op tions.add(Forma tTheDateAsASele ctOptionThing(t huDate));
      }
      }

      function FormatTheDateAs ASelectOptionTh ing(d)
      {
      var aWeekDayNames =
      new Array('Sunday', 'Monday','Tuesd ay','Wednesday' ,'Thursday','Fr iday','Saturday ');

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

      var sValue = 'for ' + sText;

      return new Option(sText, sValue);
      }

      window.onload = FillThemDateOpt ions;

      </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

      Comment

      • Dr John Stockton

        #4
        Re: Convert from VBScript...

        JRS: In article <MPG.1b7828968f 2f93d498a00f@nn tp.lucent.com>, dated
        Mon, 2 Aug 2004 11:32:31, seen in news:comp.lang. javascript, kaeli
        <tiny_one@NOSPA M.comcast.net> posted :[color=blue]
        >In article <c178e3e8.04080 20656.51cb9aa3@ posting.google. com>, david@scene-
        >double.co.uk enlightened us with...[/color]
        [color=blue][color=green]
        >> 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.[/color][/color]

        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.

        [color=blue]
        >I checked this in IE. Should work in Netscape, too. Check other browsers as
        >needed. Watch for word-wrap.[/color]

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

        [color=blue]
        >function addDays(t_date, days)
        > {
        > return new Date(t_date.get Time() + days*24*60*60*1 000);
        > }[/color]


        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.


        [color=blue]
        >function fillIt()[/color]
        [color=blue]
        > var theDate = new Date();[/color]
        [color=blue]
        > theDate = addDays(theDate ,1);[/color]


        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.get Date()+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.

        --
        © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
        <URL:http://jibbering.com/faq/> JL / RC : FAQ for news:comp.lang. javascript
        <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
        <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

        Comment

        • David Gordon

          #5
          Re: Convert from VBScript...

          Ben,

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

          </script>


          </head>

          <body onLoad = "FillThemDateOp tions()">
          <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 ?



          *** Sent via Developersdex http://www.developersdex.com ***
          Don't just participate in USENET...get rewarded for it!

          Comment

          • David Gordon

            #6
            Re: Convert from VBScript...

            Hi,

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

            <SCRIPT language='vbscr ipt'>
            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.Da te.Options
            For Count = 1 To 20


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


            StrDate2 = "Thursday " & Right("0" & Day(TheDate2),2 ) & "/" & Right("0"
            &_
            Month(TheDate2) ,2) & "/" & Year(TheDate2)
            Options.Add Window.Option(S trDate2,"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.



            *** Sent via Developersdex http://www.developersdex.com ***
            Don't just participate in USENET...get rewarded for it!

            Comment

            • Dr John Stockton

              #7
              Re: Convert from VBScript...

              JRS: In article <410fb2fb$0$559 9$c397aba@news. newsgroups.ws>, dated
              Tue, 3 Aug 2004 15:45:00, seen in news:comp.lang. javascript, David
              Gordon <david@scene-double.co.uk> posted :[color=blue]
              >Hi,
              >
              >Just to let you know that I suddenly had a brainwave with my original
              >code and changed it to:
              >
              ><SCRIPT language='vbscr ipt'>
              >Sub Window_Onload
              > Dim TheDate
              > Dim Count
              > Dim Options
              > TheDate = Date + vbTuesday - WeekDay(Date)
              > TheDate2 = Date + vbThursday - WeekDay(Date)[/color]

              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.
              [color=blue]
              >If TheDate < Date Then TheDate = TheDate + 7[/color]

              If TheDate2 < Date ... ' also?

              Alternatively, Date + (7 + vbTuesday - WeekDay(Date)) mod 7
              or Date + (6 + vbTuesday - WeekDay(Date)) mod 7 + 1
              [color=blue]
              > Set Options = Document.All.Da te.Options
              > For Count = 1 To 20[/color]

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

              or Right(100 + Day(TheDate), 2) . IMHO,
              that should be made a function, for legibility and modularity.
              [color=blue]
              >*** Sent via Developersdex http://www.developersdex.com ***
              >Don't just participate in USENET...get rewarded for it![/color]

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

              --
              © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
              <URL:http://jibbering.com/faq/> JL / RC : FAQ for news:comp.lang. javascript
              <URL:http://www.merlyn.demo n.co.uk/vb-dates.htm> VB maths, dates, sources.
              <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

              Comment

              Working...