Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Variable text dependant on day of month 2

Status
Not open for further replies.

CharlieBabbage

Technical User
Dec 26, 2006
17
GB
I'm trying to get text into a web page. The text will vary each day, depending on the current day of the month.

Why does the following only display the text for the 1st day of the month whereas today is the 26th day at the time of posting? When I first wrote this code I did not start with 'if (daynow=1)' but with '(daynow=26)' and it worked fine.

<code>
<SCRIPT LANGUAGE="JavaScript">
datetoday= new Date()
daynow=datetoday.getDate()
if (daynow=1) text="This is the text for the 1st of the month";
else if (daynow=26) text="This is the text for the 26th of the month";
else if (daynow=27) text="This is the text for the 27th of the month";
else if (daynow=28) text="This is the text for the 28th of the month";
document.write(text)
// -->
</script>
</code>
 
Here you go.

<SCRIPT LANGUAGE="JavaScript">
datetoday= new Date();
daynow=datetoday.getDate();
document.write(daynow);
if (daynow=="1"){
text="This is the text for the 1st of the month";
}else if (daynow==26){
text="This is the text for the 26th of the month";
}else if (daynow==27){
text="This is the text for the 27th of the month";
}else if (daynow==28){
text="This is the text for the 28th of the month";
}

document.write(text)

// -->
</script>

with (daynow=1) you were making daynow equal 1.
with (daynow==1) your saying is daynow equal to 1. Comparison in javascript.
 
Many thanks for your coding and apologies for the delay in replying.

Unfortunately, I cannot get this code to work and no doubt I'm doing something wrong! The first point I've noticed from your code is that in line 5 you have if (daynow=="1", whereas in line 7 you have "}else if (daynow==26){". Should the number following "==" always be in double inverted commas?

The other point is that in line 4 you have "document.write(daynow);"Presumbly this should print the current day number? In fact, once I did manage to get output e.g. "17This is the text for the 17th day of the month". I now cannot get anything at all! The output I want is "This is the text for the 17th day of the month"

This is what I have done:

I have created the following file in Notepad and saved it as 'varibletext.htm'

<html>
<body>
<br>
<br>
<SCRIPT LANGUAGE="JavaScript">
datetoday= new Date();
if (daynow=="1"){
text="This is the text for the 1st of the month";
}else if (daynow=="16"){
text="This is the text for the 16th of the month";
}else if (daynow=="17"){
text="This is the text for the 17th of the month";
}else if (daynow=="26"){
text="This is the text for the 26th of the month";
}else if (daynow=="27"){
text="This is the text for the 27th of the month";
}else if (daynow=="28"){
text="This is the text for the 28th of the month";
}

document.write(text)

// -->
</script>
</body>
</html>

When I double click on 'variabletext.htm' IE v 6.0 opens with the warning that the file contains Active X. I accept all the warnings and then a blank page is displayed. Is there something else I need to do to get the text to display?

Thanks again for your help.
 
You never define daynow, so the value is undefined, which means that none of your if/else statements evaluate to true.

Is this a homework/schoolwork project?

Lee
 
Thanks trollacious! No, it is not a homework/schoolwork project!!
 
I use something like this for ordinal numbers.
Code:
var ordinal;
switch (number % 10)
  {
  case 1:
    {
    ordinal = 'st';
    break;
    }
  case 2:
    {
    ordinal = 'st';
    break;
    }
  case 3:
    {
    ordinal = 'rd';
    break;
    }
  default:
    {
    ordinal = 'th';
    break;
    }
  }
var text="This is the text for the ' + number + ordinal + ' of the month";

Lee
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top