Show different text -- depends on the current date

L

Luigi

Imagine I have a small, static, personal site (I do, actually:
http://kirpi.it/).
And imagine that, just below the title of the page, I would like to
have a comment, or a news line, or a birthday reminder, a citation, or
whatever you like.
At the moment you will find a line that reminds about my small child's
second birthday, just as an example.
My choice is:
a) set it manually every day as I wake up, before breakfast, or
b) have it set by the system, automatically.

What I thought is: I can write a document, let's call it "facts.txt",
and have it sit somewhere inside my site. Such a page would probably
look like this, maybe:
0101 This is the first day of the year.
0201 Happy Birthday, Dad!
0301 Travel is fatal to prejudice, bigotry and narrow-mindedness.
[Mark Twain]
0401 ...

Now, my problem: is there a simple way to grab the right line, based
on the current date, and have it displayed properly?
Or, failing that, is there a better way you can think of?

Thanks
Luigi
 
M

Martin Honnen

Luigi said:
Imagine I have a small, static, personal site (I do, actually:
http://kirpi.it/).

What I thought is: I can write a document, let's call it "facts.txt",
and have it sit somewhere inside my site. Such a page would probably
look like this, maybe:
0101 This is the first day of the year.
0201 Happy Birthday, Dad!
0301 Travel is fatal to prejudice, bigotry and narrow-mindedness.
[Mark Twain]
0401 ...

Well you could use a script file with an array e.g.
var quotes = [
'This is the first day of the year.',
'Happy Birthday, Dad!',
'Travel is fatal to prejudice, bigotry and narrow-mindedness.'
];
include that in the HTML page
<script type="text/javascript" src="quotes.js"></script>
and then have another script find the current date
var today = new Date();
and use the methods of the Date object to find the right index in the
array and output the quote.
Or put an object into the file e.g.
var quotes = {
'0101': 'This is the first day of the year.',
'0201': ''Happy Birthday, Dad!',
}
that might be easier to organize.
 
L

Luigi

If you store the information as a JS array, it becomes much
simpler to extract the information and present it in a page.

Andrew and Martin converge to this point: I'd better store the whole
list as an array.
Actually, I run a small site just because I have something to share,
but I am not a computer expert, and hardly understand what an array
is. I will study it, if the need comes. But there are two things that
are not clear to me, yet:
a) can I store the list in a *separate* document, not to burden too
much every single page with the same, stupid bulk of lines? Or am I
forced to repeat on every single page of the site the same whole list?
b) How really, practically, can I get a single phrase printed on the
page?

Maybe some of the answers were somehow present in your prompt [thank
you!] replies, but you both speak a sort of technical jargon with very
specialized terms, and I could not properly understand everything.

Luigi
 
M

Mick White

Luigi wrote:

[snip]
Maybe some of the answers were somehow present in your prompt [thank
you!] replies, but you both speak a sort of technical jargon with very
specialized terms, and I could not properly understand everything.

for instance:

<script>
q=
["1130 This is the first day of the year.",
"1201 Happy Birthday, Dad!",
"1202 Travel is fatal to prejudice, bigotry and narrow-mindedness.-Mark
Twain"]

function getToday(){
var t=new Date();
var m=t.getMonth()+1;
var d=t.getDate();
m=m<10?"0"+m:m;
d=d<10?"0"+d:d;
return ""+m+d;//US convention
}

function getQuote(){
var qq=q.length
while(qq--){
if(q[qq].substring(0,4)==getToday()){
return q[qq].substring(5);
}
}
return "";
}
document.write(getQuote())
</script>
Mick
 
L

Luigi

Your answers look quite interesting!
I will try them as soon as I have some spare time, and will let you
know.
What can I give back to the community for such a kind help? Maybe
nothing technical, I'm afraid. Anyway I just posted one of small son's
smiling images, in case somebody likes it: http://kirpi.it/.
Luigi
 
L

Luigi

I tried and follow you.
I now have http://kirpi.it/qtdata.js, where Mick's file is saved. I
set data this way:
1201 for the first day of December
1202 for the second day of December
1203 for the first day of December
This is maybe the way American adopt; I read something inside Mick's
file which was about some "US convention".

Then I pasted Andrew's suggestions in my page [http://kirpi.it], where
<script type='text/javascript'>
getQuote();
</script>
is about the bottom of the page.

Sad I hoped to see something, as today is here the second day of
December.
But nothing gets written on the screen.

Luigi
 
M

Mick White

Luigi said:
Your answers look quite interesting!
I will try them as soon as I have some spare time, and will let you
know.
What can I give back to the community for such a kind help? Maybe
nothing technical, I'm afraid. Anyway I just posted one of small son's
smiling images, in case somebody likes it: http://kirpi.it/.
Luigi
Replace this:
<script type='text/javascript'>
getQuote();
</script>

with this:
<script type='text/javascript'>
document.write(getQuote());
</script>
Mick
 
M

Mick White

Luigi said:
Then I pasted Andrew's suggestions in my page [http://kirpi.it], where
<script type='text/javascript'>
getQuote();
</script>
is about the bottom of the page.

Sad I hoped to see something, as today is here the second day of
December.
But nothing gets written on the screen.


See my previous post.
Mick
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated
Tue, 30 Nov 2004 16:19:28, seen in Luigi
Andrew and Martin converge to this point: I'd better store the whole
list as an array.

You do better to store it as an Object, because it then has the
semblance of direct access. Martin suggested that.

var X = {
'0101': 'This is the first day of the year.',
'0102': 'Happy Birthday, Dad!',
'0103': 'Travel is fatal ... [Mark Twain]',
'1202': '...',
Dummy:'' }

with (new Date())
S = String(10100+getMonth()*100+getDate()).substring(1)
Y = X // apparent direct access


X has that layout for convenient auto-editing of an existing list; all
data lines have the same punctuation.

The date-of-year should be rendered as MMDD, in accordance with ISO 8601
and with reason.

The whole of the above can be put in a *.js Include file, invoked like
<script type="text/javascript" src="include1.js"></script>
in each page; each page then "knows" Y.


It's an amusing exercise; but it means that every visitor, even if on
dial-up or slow radio link, is forced to load all the data. You could
perhaps have a different data file for each month, and compute the
include file name for the current month ... . ISTM a waste.
 
D

Dr John Stockton

JRS: In article <[email protected]>,
dated Wed, 1 Dec 2004 15:40:38, seen in Luigi
I
set data this way:
1201 for the first day of December
1202 for the second day of December
1203 for the first day of December
This is maybe the way American adopt; I read something inside Mick's
file which was about some "US convention".

Not specifically US. In writing the date in FFF, with the year at the
wrong end, the Americans write dates such that if the year is omitted
what remains complies inadvertently, no doubt) with the international
standard ISO 8601. The ISO standard order is Y M D - biggest first, just
as in h:m:s or dollars & cents or £ s d or Lire & centimos or euros &
cents.

Traditional custom outside US-afflicted regions is, in the rational Far
East, to precede ISO in using Y M D; and elsewhere, merely to reverse
the order to D M Y, as is common in postal addresses.
 
K

kirpi

So I tried, but it seems that I did something wrong: nothing shows on
my home page [http://kirpi.it]. You all can check the page source and
the extra file [http://kirpi.it/qtdata.js].
I do not understand.

You do better to store it as an Object, because it then has the
semblance of direct access. Martin suggested that.

var X = {
'0101': 'This is the first day of the year.',
'0102': 'Happy Birthday, Dad!',
'0103': 'Travel is fatal ... [Mark Twain]',
'1202': '...',
Dummy:'' }

with (new Date())
S = String(10100+getMonth()*100+getDate()).substring(1)
Y = X // apparent direct access

X has that layout for convenient auto-editing of an existing list; all
data lines have the same punctuation.

The whole of the above can be put in a *.js Include file, invoked like
<script type="text/javascript" src="include1.js"></script>
in each page; each page then "knows" Y.


John, first of all thank you for the infos about dates.
As for your lines above, I must confess that they are like chinese to
me: as you tell about "objects", "auto-editing" and the like,...
If I can find something that work, I will adopt it, but completely
trust in you all about the technical side of it.
What exactly should I paste, please?

It's an amusing exercise; but it means that every visitor, even if on
dial-up or slow radio link, is forced to load all the data. You could
perhaps have a different data file for each month, and compute the
include file name for the current month ... . ISTM a waste.

What do you mean?
The whole thing is an amusing exercise, or your last "object"
suggestion is?
The idea to break the big file into smaller ones could be interesting,
but not faisible by me.
"ISTM"? Please, realize that my mother tongue is not english, sorry.
What is a waste, then?
Luigi
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top