By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,882 Members | 2,482 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,882 IT Pros & Developers. It's quick & easy.

HELP - "On This Day" javascript code

P: n/a
Have done searches for similar questions, but cannot find anything. Nor can
I find any resources via Google (Javascript Source etc).

Thinking about implementing a topical "On This Day" feature on my site,
where there would be a paragraph produced by Javascript to say:

1995: Something notable happened
2001: Something else of note happened

I found something similar on http://www.freesticky.com, but it isn't really
versatile enough for my liking as you have to put cumbersome credits on it
etc.

I'm not particularly Javascript-savvy, but realise that the code should:

- Check date
- Look at list of dates/events*
- Output the (earlier mentioned) text

* If possible, have the possibility of having two events for the same date,
although this is not life-threatening.

Please can you help? Remember - be gentle, I don't know much.

Thanks,
Marcus
Jul 20 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
"Marcus Sheen [UK]" <no@email.please> writes:
Have done searches for similar questions, but cannot find anything. Nor can
I find any resources via Google (Javascript Source etc).

Thinking about implementing a topical "On This Day" feature on my site,
where there would be a paragraph produced by Javascript to say:

1995: Something notable happened
2001: Something else of note happened

I found something similar on http://www.freesticky.com, but it isn't really
versatile enough for my liking as you have to put cumbersome credits on it
etc.
Since you don't give a reference to the script itself, and I won't go
searching an unknown site for an unknown script, you will have to tell
me what features you want (i.e., why it isn't versatile enough)>
I'm not particularly Javascript-savvy, but realise that the code should:

- Check date
- Look at list of dates/events*
- Output the (earlier mentioned) text

* If possible, have the possibility of having two events for the same date,
although this is not life-threatening.


Easy.

<script type="text/javascript">
/** Place this script where you want the text in the document body **/
/* check data */
var now = new Date();
var today = (now.getMonth()+1)+"/"+now.getDate();

/* look at list of dates/events */
/* list: */
var eventList = [
["7/4","Independence Day in the US"],
["3/28","Someone's birthday"],
["11/9","The day this script was written"]
];

/* look through ... */
for (var i=0;i<eventList.length;i++) {
if (eventList[i][0] == today) {
/* ... and output */
document.write("<p>TODAY: "+eventList[i][1]+"<\/p>");
}
}
</script>

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2

P: n/a
JRS: In article <is**********@hotpop.com>, seen in
news:comp.lang.javascript, Lasse Reichstein Nielsen <lr*@hotpop.com>
posted at Sun, 9 Nov 2003 18:40:55 :-
"Marcus Sheen [UK]" <no@email.please> writes:
Thinking about implementing a topical "On This Day" feature on my site,
where there would be a paragraph produced by Javascript to say:

var now = new Date();
var today = (now.getMonth()+1)+"/"+now.getDate();
The separator should be "-", not "/", to imply compliance with ISO-
8601's Y-M-D rather than the USA's M/D/Y <G>.

var eventList = [
["7/4","Independence Day in the US"],
["3/28","Someone's birthday"],
["11/9","The day this script was written"]
];


AIUI, events occur only once; so the year will be of interest, birthdays
will not, but birthdates will.

One could use

var eventList = [
"1776-07-04 Independence Day in the US",

RE = new RegExp(today) // after creating today as \\d+-##-##

if ( RE.test(eventList[i]) ) ...

A more efficiently-accessed data structure might be indicated by

var List = {
D0704: ["1776 US Independence", "1998 Fred born"],
D0423: ["1564 Birth", "1616 Death"] }

today = "D0423"

if (T=List[today]) alert(T.join('\n'))

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #3

P: n/a
Dr John Stockton wrote:
Lasse Reichstein Nielsen [wrote]:
"Marcus Sheen [UK]" <no@email.please> writes:
var eventList = [
["7/4","Independence Day in the US"],
["3/28","Someone's birthday"],
["11/9","The day this script was written"]
];


AIUI, events occur only once; so the year will be of interest, birthdays
will not, but birthdates will.

One could use

var eventList = [
"1776-07-04 Independence Day in the US",

RE = new RegExp(today) // after creating today as \\d+-##-##

if ( RE.test(eventList[i]) ) ...

A more efficiently-accessed data structure might be indicated by

var List = {
D0704: ["1776 US Independence", "1998 Fred born"],
D0423: ["1564 Birth", "1616 Death"] }

today = "D0423"

if (T=List[today]) alert(T.join('\n'))


There is not need for the RegExp (untested):

function EventList_getEventsFor(d)
{
return this[d.setYear(1970)]:
}

function EventList()
{
if (typeof arguments == "undefined")
var arguments = EventList.arguments;

for (var i = 0; i < arguments.length; i++)
{
this[Date.UTC(1970, arguments[i][0], arguments[i][1])] =
arguments[i][2];
}

this.getEventsFor = EventList_getEventsFor;
}

var eventList =
new EventList(
[1, 1, [[1616, "John Doe (1564-1616) died in Foobar"]]],
[3, 23, [[1564, "John Doe (1564-1616) born in Foobar"]]],
[6, 4, [[1776, "Independence Day (USA)"]]]);

var e = eventList.getEventsFor(new Date());
if (e)
{
for (var i = 0; i < e.length; i++)
{
document.write(e[i][0] + " " + e[i][1] + "\n");
}
}

But that is a feature best to be implemented server-side.
PointedEars
Jul 20 '05 #4

P: n/a
Thomas 'Ingrid' Lahn wrote:
function EventList_getEventsFor(d)
{
return this[d.setYear(1970)]:

^
Must be a `;'.
PointedEars
Jul 20 '05 #5

P: n/a
JRS: In article <bp*************@ID-107532.news.uni-berlin.de>, seen in
news:comp.lang.javascript, Thomas 'PointedEars' Lahn
<Po*********@web.de> posted at Tue, 18 Nov 2003 00:22:51 :-
Dr John Stockton wrote:
Lasse Reichstein Nielsen [wrote]:
"Marcus Sheen [UK]" <no@email.please> writes:
var eventList = [
["7/4","Independence Day in the US"],
["3/28","Someone's birthday"],
["11/9","The day this script was written"]
];
AIUI, events occur only once; so the year will be of interest, birthdays
will not, but birthdates will.

One could use

var eventList = [
"1776-07-04 Independence Day in the US",

RE = new RegExp(today) // after creating today as \\d+-##-##

if ( RE.test(eventList[i]) ) ...

A more efficiently-accessed data structure might be indicated by

var List = {
D0704: ["1776 US Independence", "1998 Fred born"],
D0423: ["1564 Birth", "1616 Death"] }

today = "D0423"

if (T=List[today]) alert(T.join('\n'))


There is not need for the RegExp (untested):


Of course it is not necessary to use a RegExp for this; the second part
of the above does not do so. The required data is accessed directly,
with only an implicit search.

Untested code is generally non-functional code.

function EventList_getEventsFor(d)
{
return this[d.setYear(1970)]:
}
An interesting example of a disadvantage of adding [semi-] colons where
not necessary.

function EventList()
{
if (typeof arguments == "undefined")
var arguments = EventList.arguments;

for (var i = 0; i < arguments.length; i++)
{
this[Date.UTC(1970, arguments[i][0], arguments[i][1])] =
arguments[i][2];
}

this.getEventsFor = EventList_getEventsFor;
}

var eventList =
new EventList(
[1, 1, [[1616, "John Doe (1564-1616) died in Foobar"]]],
[3, 23, [[1564, "John Doe (1564-1616) born in Foobar"]]],
[6, 4, [[1776, "Independence Day (USA)"]]]);

var e = eventList.getEventsFor(new Date());
if (e)
{
for (var i = 0; i < e.length; i++)
{
document.write(e[i][0] + " " + e[i][1] + "\n");
}
}

It is not a good idea to enter dates with months 0..11; the risk of
human error is too great.

But that is a feature best to be implemented server-side.


When server-side is available. It is not so for some web sites; and
client-side means that the page can be downloaded and used off-line.

It's not clear to me what your code is supposed to do; for me, with
colon replaced, it does nothing.

This is fundamentally a pattern-matching or lookup job; it has little to
do with the properties of dates, after the appropriate month and day are
found. There is no need to use any Date Objects after that.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #6

P: n/a
Dr John Stockton wrote:
JRS: In article <bp*************@ID-107532.news.uni-berlin.de>, seen in
news:comp.lang.javascript, Thomas 'PointedEars' Lahn
<Po*********@web.de> posted at Tue, 18 Nov 2003 00:22:51 :-
Dr John Stockton wrote:
[...]
A more efficiently-accessed data structure might be indicated by

var List = {
D0704: ["1776 US Independence", "1998 Fred born"],
D0423: ["1564 Birth", "1616 Death"] }

today = "D0423"

if (T=List[today]) alert(T.join('\n'))
There is not need for the RegExp (untested):


Of course it is not necessary to use a RegExp for this; the second part
of the above does not do so.


ACK but you need to encode the date you want to access somehow
(here: "D0423") in order to access the respective property.
Untested code is generally non-functional code.
NAK
function EventList_getEventsFor(d)
{
return this[d.setYear(1970)]:
}


An interesting example of a disadvantage of adding [semi-] colons where
not necessary.


That is only a typo and I have corrected myself immediately after posted it,
spellflamer!
It is not a good idea to enter dates with months 0..11; the risk of
human error is too great.
OK, that can be changed.
But that is a feature best to be implemented server-side.


When server-side is available. It is not so for some web sites; and
client-side means that the page can be downloaded and used off-line.


True. Nevertheless the feature is not available without client-side
support for JavaScript for which the probability is greater here. We
are still talking about a *web* *site*.
It's not clear to me what your code is supposed to do; for me, with
colon replaced, it does nothing.
Debug it. It does something (the lookup) but I used the current date as
lookup argument (for the constructor function) and there are no events
assigned to today[tm], so there is no output for today[tm].
This is fundamentally a pattern-matching or lookup job; it has little to
do with the properties of dates, after the appropriate month and day are
found. There is no need to use any Date Objects after that.


It is still a lookup job but the key, i.e. the property of the object, is
now numeric (exactly the number of milliseconds from January 1st, 1970
midnight in UTC) which makes it easier and more effective to pass the
appropriate value for the lookup -- no need for further date encoding but
*plain* Date objects can be used.
PointedEars
Jul 20 '05 #7

P: n/a
JRS: In article <bp*************@ID-107532.news.uni-berlin.de>, seen in
news:comp.lang.javascript, Thomas 'PointedEars' Lahn
<Po*********@web.de> posted at Tue, 18 Nov 2003 20:48:27 :-
Dr John Stockton wrote:
JRS: In article <bp*************@ID-107532.news.uni-berlin.de>, seen in
news:comp.lang.javascript, Thomas 'PointedEars' Lahn
<Po*********@web.de> posted at Tue, 18 Nov 2003 00:22:51 :-
Dr John Stockton wrote:
[...]
A more efficiently-accessed data structure might be indicated by

var List = {
D0704: ["1776 US Independence", "1998 Fred born"],
D0423: ["1564 Birth", "1616 Death"] }

today = "D0423"

if (T=List[today]) alert(T.join('\n'))

There is not need for the RegExp (untested):


Of course it is not necessary to use a RegExp for this; the second part
of the above does not do so.


ACK but you need to encode the date you want to access somehow
(here: "D0423") in order to access the respective property.


Yes, but D0423 is a rather simple coding, needed only once and easily
read. D so that it does not start with a digit, then MM & DD. Leading
zero on month is not essential, but there must be some way to decide
whether 112 is Jan 12 or Nov 2.

It's not clear to me what your code is supposed to do; for me, with
colon replaced, it does nothing.


Debug it. It does something (the lookup) but I used the current date as
lookup argument (for the constructor function) and there are no events
assigned to today[tm], so there is no output for today[tm].


Yes, I allowed for that.

Perhaps your code needs Feature Detection to deal with browsers other
than that/those you tested with.

BTW, 04-23 applies to someone more famous than John Doe; and I don't
mean St. George.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #8

P: n/a
Dr John Stockton wrote:
JRS: In article <bp*************@ID-107532.news.uni-berlin.de>, seen in
news:comp.lang.javascript, Thomas 'PointedEars' Lahn <Po*********@web.de>
posted at Tue, 18 Nov 2003 20:48:27 :-
Could you shorten that, please?
Dr John Stockton wrote:
Of course it is not necessary to use a RegExp for this; the second
part of the above does not do so.


ACK but you need to encode the date you want to access somehow (here:
"D0423") in order to access the respective property.


Yes, but D0423 is a rather simple coding, needed only once and easily
read. D so that it does not start with a digit,


Although there is this restriction for JavaScript identifiers, I found it
useful that it does not apply to properties when referenced via the index
operator. That is why it is, or at least seems to be, possible to use the
Date serial number for their name and have the lookup based on the Date
object itself.
then MM & DD. Leading zero on month is not essential, but there must be
some way to decide whether 112 is Jan 12 or Nov 2.
I agree but note that the OP asked for an "On This Day" script which reads
to me like an *automated* script. Sure you can create the above code using
the Date object _and_ string operations but it is more efficient to use the
Date (object), i.e. the serial number stored for it internally, itself.
Perhaps your code needs Feature Detection to deal with browsers other
than that/those you tested with.
Perhaps. Do you get any script errors? With what UA(s) were/are you testing?
BTW, 04-23 applies to someone more famous than John Doe; and I don't mean
St. George.


Forgive my ignorance, I have no out-of-the-box-idea who could be meant here.
PointedEars
Jul 20 '05 #9

P: n/a
JRS: In article <bp*************@ID-107532.news.uni-berlin.de>, seen in
news:comp.lang.javascript, Thomas 'PointedEars' Lahn
<Po*********@web.de> posted at Thu, 20 Nov 2003 16:22:14 :-
Dr John Stockton wrote:
JRS: In article <bp*************@ID-107532.news.uni-berlin.de>, seen in
news:comp.lang.javascript, Thomas 'PointedEars' Lahn <Po*********@web.de>
posted at Tue, 18 Nov 2003 20:48:27 :-


Could you shorten that, please?


I have the technology but not the inclination.
Yes, but D0423 is a rather simple coding, needed only once and easily
read. D so that it does not start with a digit,


Although there is this restriction for JavaScript identifiers, I found it
useful that it does not apply to properties when referenced via the index
operator. That is why it is, or at least seems to be, possible to use the
Date serial number for their name and have the lookup based on the Date
object itself.


Agreed; one would be looking up a sparse array with at most 366 elements
in the range 0 to 31536000000.
then MM & DD. Leading zero on month is not essential, but there must be
some way to decide whether 112 is Jan 12 or Nov 2.


I agree but note that the OP asked for an "On This Day" script which reads
to me like an *automated* script. Sure you can create the above code using
the Date object _and_ string operations but it is more efficient to use the
Date (object), i.e. the serial number stored for it internally, itself.


ISTM better to use a Date Object for the probably-few dates asked about
than for the probably-many listed. Also that, in practice, the
difference will be small; but I prefer to be able to scan the data
readable in the source than a processed form.
Perhaps your code needs Feature Detection to deal with browsers other
than that/those you tested with.


Perhaps. Do you get any script errors? With what UA(s) were/are you testing?


No errors; the code appears to do nothing at all.

I suspect that the code, in the form in which it was posted to News, had
not been tested, except perhaps at midnight. The colon error supports
this; moreover, if I change the first function to either one of

function EventList_getEventsFor(d) {
return this[new Date(d.setYear(1970)).setHours(0,0,0,0)] }

function EventList_getEventsFor(d) {
return this[Date.UTC(1970, d.getMonth(), d.getDate())] }
and alter one of the events (for testing today) to [10, 20, [[1564, ...
then it does work.

My view is that, before posting to News, there is no need to test code
of more than about a line in length in order to see whether it works;
one can be reasonably certain that the code, if untested, does not work.
If the code is tested, however, it is more likely to work first time.
BTW, 04-23 applies to someone more famous than John Doe; and I don't mean
St. George.


Forgive my ignorance, I have no out-of-the-box-idea who could be meant here.


"There is a history in all men's lives" --- but this man is notable not
only for his histories. "Cudgel thy brains no more about it"; 'twas the
Bard of Avon.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.