Saturdays

Discussion in 'Javascript' started by Maarten, Jan 19, 2004.

  1. Maarten

    Maarten Guest

    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.
    Maarten, Jan 19, 2004
    #1
    1. Advertising

  2. Maarten

    Lee Guest

    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>
    Lee, Jan 19, 2004
    #2
    1. Advertising

  3. Maarten wrote:

    > 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>");
    }
    Brian Genisio, Jan 19, 2004
    #3
  4. Brian Genisio <> writes:

    > 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
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
    Lasse Reichstein Nielsen, Jan 19, 2004
    #4
  5. Maarten wrote:
    > 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
    Janwillem Borleffs, Jan 19, 2004
    #5
  6. Maarten

    Maarten Guest

    Thanx everybody. Fine to find help here. Maarten - Belgium.
    Maarten, Jan 20, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. gunso
    Replies:
    10
    Views:
    214
    Dr J R Stockton
    May 12, 2009
Loading...

Share This Page