...

View Full Version : code for message of the day



lucromick
10-01-2011, 07:30 PM
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.



<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>

Philip M
10-01-2011, 07:49 PM
You need to revise your understanding of the syntax of a switch statement.
Also <script type="text/javascript">




<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

xelawho
10-01-2011, 08:02 PM
or simpler:



<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>

nomanic
10-01-2011, 08:20 PM
battle for simplest!



<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>

ironboy
10-01-2011, 08:31 PM
Joining the battle :rolleyes:

function getWeekday() {
return "Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday".split(',')[new Date().getDay()]
};

nomanic
10-01-2011, 08:35 PM
love it!

lucromick
10-01-2011, 08:44 PM
Thank you for all the help guys! I'll use the DOM method from now on.
much appreciated :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum