www.webdeveloper.com
Results 1 to 2 of 2

Thread: If statement with dates

  1. #1
    Join Date
    Apr 2012
    Posts
    1

    Arrow If statement with dates

    Hello everyone! I am 100% completely new to JavaScript. I am trying to complete some JavaScript for an intro JavaScript class.

    In a nutshell: I am displaying the date my fiance and I got engaged, am utilizing a button to reveal our wedding date, and would like to display a message if the current date is past the set wedding date. The problem I am having is with displaying a message if the current date is past the wedding date.

    I tested it out by changing the set wedding date to be in the past (I just changed the year to be 2009 temporarily) but the message appeared on the page. I tried playing around with it more, searching for more info using if statements for dates, so on and so forth. I am just coding myself in circles at this point and feel totally lost (hence my username!). What am I doing wrong? How do I make this work?

    Unfortunately I do not have a test page as I do not have a website yet. Hoping the code is simple enough that posting it below will suffice. Thanks in advance for any help!

    Code:
    <!DOCTYPE HTML>
    <head>
    <meta charset="utf-8">
    <title>Wedding Date Reveal</title>
    <script>
    <!--
    
    function weddingDate()
        {
          var wd = new Date();
          wd.setFullYear(2013,0,5);
    
          var reveal = document.getElementById("weddingdate_reveal");
          reveal.innerHTML=wd;
         }
    
    function afterWedding()
        {
          var aw = new Date();
    
          if  (aw>wd) {
    	var afterWedding = document.getElementById("after_wedding");write("Sorry, our wedding date has passed! It was on January 5, 2013.");
    	afterWedding.innerHTML=aw;
                           }
        }
    
    //-->
    </script>
    </head>
    
    <body>
    
    <p>We got engaged on 
    <script type="application/javascript">
    var ed = new Date();
    ed.setFullYear(2010,10,24);
    document.write(ed)
    </script>
    
    </p>
    <p>We are getting married on ... <br />
    <button onclick="weddingDate()">Click for the date!</button> </p>
    <p id="weddingdate_reveal"></p>
    
    <p id="after_wedding"></p>
    
    </body>
    </html>

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,373

    Lightbulb

    Slight re-write...
    Code:
    <!DOCTYPE HTML>
    <head>
    <meta charset="utf-8">
    <title>Wedding Date Reveal</title>
    <script type="text/javascript">
      var ed = new Date(2010,10,24);
      var wd = new Date(2013,0,5);  // change year value for testing purposes
      var now = new Date();
    function weddingDate() {
      document.getElementById('weddingdate_reveal').innerHTML = wd.toDateString();
    }
    function weddingDate() {
      var msg = wd.toDateString();
      if  (now > wd) {
        msg = 'Sorry, our wedding date has passed!  It was on '+msg;
      }
      document.getElementById('weddingdate_reveal').innerHTML = msg;
    //  alert(now + '\n' + wd);  // for testing purposes
    }
    window.onload = function() {
      document.getElementById('engagedOn').innerHTML = 'We got engaged on '+ed.toDateString();
    }
    </script>
    </head>
    <body>
    <p id="engagedOn"></p>
    <p>We are getting married on ... <br />
    <button onclick="weddingDate()">Click for the date!</button> </p>
    <p id="weddingdate_reveal"></p>
    </body>
    </html>
    You can fancy it up further with some CSS changes
    or substitute a different function for the 'toDateString()'

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles