Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Nov 2006
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Self updating date drop down box

    I'm trying to make a drop down box for the date mont and year for my website. However the days in february are 28 or 29 but the other months have either 30 or 31. How can i make a script that enables the date drop down box to change to the appropriate number of days,say if february was selected or if december was selected also considering leaps years and stuff

  • #2
    Banned
    Join Date
    Oct 2005
    Location
    I'm in GMT -5
    Posts
    314
    Thanks
    0
    Thanked 1 Time in 1 Post
    simbarashe:

    Start by writing "please" and "thank you."

  • #3
    New Coder
    Join Date
    Nov 2006
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts
    sorry about that I was rushing to work and a bit frustrated coz I had tried everything I couold think of. I'm really sorry, may you please advise me on how I can go abput solving my problem

    Thank you

  • #4
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Try this example for finding the number of days in a month. There may be a better way but I don't know it.

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html>
      <head>
        <title>Calc Days in Month</title>
        <script type="text/javascript">
          function doIt(s) {
            var adiv = document.getElementById("adiv");
            adiv.innerHTML = s;
          }
    
          function calcDays(mon, year) {
            var dt = new Date();
            var m = parseInt(mon) - 1; // months for Date() are from 0 to 11
            var y = parseInt(year);
    
            for (var i = 1; i < 5; i++) {
              dt.setFullYear(y, m, 28 + i); // assume each month has at least 28 days
              if (dt.getMonth() != m) {
                break;
              }
            }
    
            return(28 + i - 1);
          }
        </script>
      </head>
      <body>
        <div>
          <form name="f" action="#" onsubmit="return(false);">
            <label>Month <input type="text" name="m" value="12"></label>
            <br />
            <label>Year <input type="text" name="y" value="2006"></label>
          </form>
          <button onclick="doIt(calcDays(document.f.m.value, document.f.y.value));">Calc Num Days</button>
          <br />
          <div id="adiv"> </div>
        </div>
      </body>
    </html>
    david_kw


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •