Saturdays

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

    Saturdays

    Hi,

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

    The code must be like:

    <select class="fld" size="1" name="arrivalda te">
    <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.


  • Brian Genisio

    #2
    Re: Saturdays

    Maarten wrote:
    [color=blue]
    > Hi,
    >
    > i need to fill a dropdownlist in a frame with only saturdays.
    >
    > The code must be like:
    >
    > <select class="fld" size="1" name="arrivalda te">
    > <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.
    >
    >[/color]

    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.setTim e( today.getTime() + dayOffset + (7 * i * msInDay) );
    document.write( saturday.toDate String() + "<BR>");
    }

    Comment

    • Lee

      #3
      Re: Saturdays

      Maarten said:[color=blue]
      >
      >Hi,
      >
      >i need to fill a dropdownlist in a frame with only saturdays.
      >
      >The code must be like:
      >
      ><select class="fld" size="1" name="arrivalda te">
      ><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?[/color]

      <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.getFul lYear()+1,0,1);
      // nextYear is the first day of next year
      var saturday=new Date(now.setDat e(now.getDate() +6-now.getDay()));
      // saturday is now the first Saturday after yesterday.
      while(saturday< nextYear){
      var DDMMYYYY=zp(sat urday.getDate() )+"/"+
      zp(saturday.get Month()+1)+"/"+
      saturday.getFul lYear();
      saturdays[saturdays.lengt h]="<option value=\""+
      DDMMYYYY+"\">"+
      DDMMYYYY+"</option>";
      saturday.setDat e(saturday.getD ate()+7);
      }
      </script>
      </head>
      <body>
      <form>
      <select>
      <script type="text/javascript">
      document.write( saturdays.join( ""));
      </script>
      </select>
      </form>
      </body>
      </html>

      Comment

      • Lasse Reichstein Nielsen

        #4
        Re: Saturdays

        Brian Genisio <BrianGenisio@y ahoo.com> writes:
        [color=blue]
        > Here is a quick way to do it... I am sure there are others...
        >
        > var msInDay = 1000 * 60 * 60 * 24;[/color]

        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.
        [color=blue]
        > // Get today's date
        > var today = new Date();
        >
        > // Set the date to saturday 0 = Sunday to 6 = Saturday
        > var dayOffset = (6 - today.getDay()) * msInDay;[/color]

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

        Shorter version:

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

        /L
        --
        Lasse Reichstein Nielsen - lrn@hotpop.com
        DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
        'Faith without judgement merely degrades the spirit divine.'

        Comment

        • Janwillem Borleffs

          #5
          Re: Saturdays

          Maarten wrote:[color=blue]
          > i need to fill a dropdownlist in a frame with only saturdays.
          >
          > The code must be like:
          >
          > <select class="fld" size="1" name="arrivalda te">
          > <option value="17/01/2004">17/01/2004</option>[/color]

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

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

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

          document.writel n(
          '<option value="', date, '">', date, '</option>'
          );
          d.setDate(d.get Date() + 1);
          }
          </script>


          HTH;
          JW



          Comment

          • Maarten

            #6
            Re: Saturdays

            Thanx everybody. Fine to find help here. Maarten - Belgium.


            Comment

            Working...