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 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Aug 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    code for message of the day

    Hello all,

    I'm trying to write a simple script in JS that will show a message for each day of the week. I have searched in your forums and other places but sadly I keep failing.

    Thank you for helping a newb.

    Code:
    <script type="JavaScript">
    
    
    function showMessage()
    {
        var d = new Date();
        var day = d.getDay();     // Note: Sunday=0, Monday=1, etc...
    
        switch( true )
       
            }
            case ( day == 6 ) :
            {
              document.write("Today is saturday!")
            }
             case ( day == 5 ) :
            {
              document.write("Today is friday!")
            }
             case ( day == 4 ) :
            {
              document.write("Today is thursday!")
            }
             case ( day == 3 ) :
            {
              document.write("Today is wednesday!")
            }
             case ( day == 2 ) :
            {
              document.write("Today is tuesday!")
            }
             case ( day == 1 ) :
            {
              document.write("Today is monday!")
            }
             case ( day == 0 ) :
            {
              document.write("Today is sunday!")
            }
        }    
    }
    
        </script>

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,081
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    You need to revise your understanding of the syntax of a switch statement.
    Also <script type="text/javascript">


    Code:
    <html>
    <head>
    
    <script type="text/javascript">
    function showMessage() {
    var d = new Date();
    var day = d.getDay();     // Note: Sunday=0, Monday=1, etc...
    var message = "";
    switch(true){
    case ( day == 6 ) : message = "Saturday" ; break;
    case ( day == 5 ) : message = "Friday" ; break;
    case ( day == 4 ) : message = "Thursday" ; break;
    case ( day == 3 ) : message = "Wednesday" ; break;
    case ( day == 2 ) : message = "Tuesday" ; break;
    case ( day == 1 ) : message = "Monday" ; break;
    case ( day == 0 ) : message = "Sunday" ; break;
    }   
    
    document.getElementById("display").innerHTML = "Today is " + message;
    }
    </script> 
    </head>
    
    <body onload = "showMessage()">
          
    <span id = "display" style=color:"red"></span>
    
    </body>
    </html>
    Prefer to use DOM methods of displaying the information which can be styled using css rather than the crude document.write method.


    Correct me if I'm not mistaken - Politician interviewed on BBC Radio Four
    Last edited by Philip M; 10-01-2011 at 07:53 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,981
    Thanks
    56
    Thanked 557 Times in 554 Posts
    or simpler:

    Code:
    <script type="text/javascript">
    function showMessage() {
    var d = new Date();
    var day = d.getDay();     // Note: Sunday=0, Monday=1, etc...
    var message = "";
    switch(day){
    case 6: message = "Saturday" ; break;
    case 5: message = "Friday" ; break;
    case 4: message = "Thursday" ; break;
    case 3: message = "Wednesday" ; break;
    case 2: message = "Tuesday" ; break;
    case 1: message = "Monday" ; break;
    case 0: message = "Sunday" ; break;
    default : message = "wha?" ; break;
    }   
    
    document.getElementById("display").innerHTML = "Today is " + message;
    }
    </script> 
    </head>
    
    <body onload = "showMessage()">
          
    <span id = "display" style=color:"red"></span>
    
    </body>
    </html>

  • #4
    Regular Coder nomanic's Avatar
    Join Date
    Feb 2009
    Location
    United Kingdom
    Posts
    255
    Thanks
    9
    Thanked 33 Times in 33 Posts
    battle for simplest!

    Code:
    <script type="text/javascript">
    function getWeekday() {
         switch((new Date()).getDay()){
         case 6: return "Saturday" ;
         case 5: return "Friday" ;
         case 4: return "Thursday" ;
         case 3: return "Wednesday" ;
         case 2: return "Tuesday" ;
         case 1: return "Monday" ;
         }   
         return "Sunday" ;
    };
    function showMessage() {
         document.getElementById("display").innerHTML = "Today is " + getWeekday();
    };
    </script> 
    </head>
    <body onload = "showMessage()">      
    <span id = "display" style=color:"red"></span>
    </body>
    </html>
    Last edited by nomanic; 10-01-2011 at 08:23 PM. Reason: better spacing

  • #5
    Regular Coder
    Join Date
    Sep 2011
    Location
    Sweden
    Posts
    154
    Thanks
    1
    Thanked 22 Times in 22 Posts
    Joining the battle
    Code:
    function getWeekday() {
      return "Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday".split(',')[new Date().getDay()]
    };
    Last edited by ironboy; 10-01-2011 at 08:43 PM.

  • #6
    Regular Coder nomanic's Avatar
    Join Date
    Feb 2009
    Location
    United Kingdom
    Posts
    255
    Thanks
    9
    Thanked 33 Times in 33 Posts
    love it!

  • #7
    New to the CF scene
    Join Date
    Aug 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you for all the help guys! I'll use the DOM method from now on.
    much appreciated


  •  

    Posting Permissions

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