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 to the CF scene
    Join Date
    May 2009
    Location
    California
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Need Help With Basic JS Homework! (Date & Custom Objects)

    Ok so here is the assignment:

    Create a Web page containing JavaScript statements and functions that perform the following:

    * Today's date should be displayed in the following format: Today is: mm/dd/yyyy
    * If the day is Monday, Tuesday, or Wednesday, display a message that says "Have a nice week!
    * If the day is Thursday or Friday, display a message that says "The weekend is almost here!"
    * If the day is Saturday or Sunday, display a message that says "Enjoy the weekend!"
    * If the date is after the 25th of a month, display a message that says "Next month is just around the corner!"

    Example:

    Today is 12/26/08
    The weekend is almost here!
    Next month is just around the corner!
    --------------------------------------------------------------------------

    Here is what I have so far:

    Code:
    <html>
    
    <head>
    
    <title>
    
    </title>
    
    </head>
    
    <body>
    
    <script type="text/javascript">
    var today = new Date();
    var dd = today.getDate();
    var mm = today.getMonth()+1;//January is 0!
    var yyyy = today.getFullYear();
    if(dd<10){dd='0'+dd}
    if(mm<10){mm='0'+mm}
    onload = function(){
    document.write('Today is: '+mm+'/'+dd+'/'+yyyy)
    }
    </script>
    
    </body>
    
    </html>
    As you can see I found out how to display the date, but as for having certain text appear based on that date, I am completely lost.

    Thanks ahead for any help, as this is due today and I am stressing!

  • #2
    Regular Coder
    Join Date
    Feb 2009
    Location
    Wheeling, IL
    Posts
    358
    Thanks
    5
    Thanked 62 Times in 60 Posts
    Have a look at the following site:

    http://www.w3schools.com/js/js_obj_date.asp

    Specifically you might be interested in the getDay() function. If you still have questions afterwards... don't hesitate to ask.
    The way to success is to assume that there are no impossible things. After all, if you think something is impossible, you will not even try to do it.

    How to ask smart questions?

  • #3
    New to the CF scene
    Join Date
    May 2009
    Location
    California
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for taking the time to help me out. I will check that out right now.

    *Edit: Here is the final code based on indirect help from a classmate.

    Code:
    <html>
    
    <head>
    
    <title>
    Test!
    </title>
    
    </head>
    
    <body>
    
    <script type="text/javascript">
    /* <![CDATA{ */
    var today=new Date();
        var dd=today.getDate();
        var mm=today.getMonth()+1;
        var yyyy=today.getFullYear();
        document.write("Today is  " + mm + "/" + dd + "/" + yyyy);
    
    var today = new Date();
    var currentDay = today.getDay();
    if (currentDay ==1 || currentDay ==2 || currentDay ==3) 
    	document.write("<p>Have a nice week!");
    else if (currentDay == 4 || currentDay == 5) 
    	document.write("<p>The weekend is almost here!");
    else if (currentDay == 6 || currentDay == 0) 
    	document.write("<p>Enjoy the weekend!");
    	
    if (dd == 26 ||27 || 28 || 29 || 30 || 31)
    	document.write("<p>Next month is just around the corner!");
    
    /* ]] */
    </script>
    
    </body>
    
    </html>
    Last edited by JSNooblet; 05-26-2009 at 11:43 PM.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,200
    Thanks
    75
    Thanked 4,342 Times in 4,308 Posts
    No, that code is *WRONG*.

    This line is NOT WORKING:
    Code:
    if (dd == 26 ||27 || 28 || 29 || 30 || 31)
    	document.write("<p>Next month is just around the corner!");
    To see that I am right, add in the line
    Code:
    dd = 1;
    just before that (in other words, pretend today is the first of the month).

    HINT: Don't test for the individual values.

    If you are interested in improving your coding skills, consider these:

    (1) Why do you need (or want) to check for currentDay being 6 or 0? If it is none of the other values, it must be one of those two.

    (2) Instead of using if...else if... consider using switch ( currentDay ) instead. See how much that makes the code clearer and simpler.

    For extra credit, have your "next month is..." message appear on only the last 5 days of the month, NO MATTER WHAT THE MONTH. (In other words, for example, in February 2010, it should appear on the 24th through 28th.)


  •  

    Posting Permissions

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