473,765 Members | 2,047 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

HELP - "On This Day" javascript code

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
9 4105
"Marcus Sheen [UK]" <no@email.pleas e> 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","Independenc e 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/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2
JRS: In article <is**********@h otpop.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.pleas e> 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","Independenc e 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(eventLi st[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.demo n.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #3
Dr John Stockton wrote:
Lasse Reichstein Nielsen [wrote]:
"Marcus Sheen [UK]" <no@email.pleas e> writes:
var eventList = [
["7/4","Independenc e 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(eventLi st[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_getEv entsFor(d)
{
return this[d.setYear(1970)]:
}

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

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

this.getEventsF or = EventList_getEv entsFor;
}

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, "Independen ce Day (USA)"]]]);

var e = eventList.getEv entsFor(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
Thomas 'Ingrid' Lahn wrote:
function EventList_getEv entsFor(d)
{
return this[d.setYear(1970)]:

^
Must be a `;'.
PointedEars
Jul 20 '05 #5
JRS: In article <bp************ *@ID-107532.news.uni-berlin.de>, seen in
news:comp.lang. javascript, Thomas 'PointedEars' Lahn
<Po*********@we b.de> posted at Tue, 18 Nov 2003 00:22:51 :-
Dr John Stockton wrote:
Lasse Reichstein Nielsen [wrote]:
"Marcus Sheen [UK]" <no@email.pleas e> writes:
var eventList = [
["7/4","Independenc e 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(eventLi st[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_getEv entsFor(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.argum ents;

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

this.getEventsF or = EventList_getEv entsFor;
}

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, "Independen ce Day (USA)"]]]);

var e = eventList.getEv entsFor(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.demo n.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #6
Dr John Stockton wrote:
JRS: In article <bp************ *@ID-107532.news.uni-berlin.de>, seen in
news:comp.lang. javascript, Thomas 'PointedEars' Lahn
<Po*********@we b.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_getEv entsFor(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
JRS: In article <bp************ *@ID-107532.news.uni-berlin.de>, seen in
news:comp.lang. javascript, Thomas 'PointedEars' Lahn
<Po*********@we b.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*********@we b.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.demo n.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #8
Dr John Stockton wrote:
JRS: In article <bp************ *@ID-107532.news.uni-berlin.de>, seen in
news:comp.lang. javascript, Thomas 'PointedEars' Lahn <Po*********@we b.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
JRS: In article <bp************ *@ID-107532.news.uni-berlin.de>, seen in
news:comp.lang. javascript, Thomas 'PointedEars' Lahn
<Po*********@we b.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*********@we b.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_getEv entsFor(d) {
return this[new Date(d.setYear( 1970)).setHours (0,0,0,0)] }

function EventList_getEv entsFor(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.demo n.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
580
by: Harman Sahni | last post by:
As per this URL http://msdn.microsoft.com/library/en-us/vjref98/html/14_14.asp?frame=true conitnue works on for, while, do... I know it works for foreach as well as I'm using it somewhere. My question is how do I "continue" (jump an iteration) of an outermost loop from an inner loop. So I have something like this:
1
2644
by: fang | last post by:
Really confusing, I am using visual stdio C++ 6.0 I add a new project file into my current workspace. If I use the default output directory like default and release, everything works fine. But after I change my output directories to some other direcotries. The errors happened: build error, The output directory can not be created! But all of the old project files whose output directories are the same
2
1288
by: timothy ma and constance lee | last post by:
I got the problem on passing the selected index of drop list to the javascript function as below: onchange:javascript:generateURL(servername, this.options..value, docID) where i want to generate something like http://www.abc.com?param?imagecursor=docID#0001
3
1614
by: Tom Thorpe | last post by:
To all wizards: I have a "C" DLL file already built and running, but I want to have the DLL accessible(callable) from anyway within a LAN and/or WAN( and even multi-WAN's), or even on a PC. I built the DLL file, via VC++'s (v6.0) ATL COM AppWizard area, and it works quite well. But as for using the DLL file on LAN's and Wan's, do I need to add any more code into the DLL file, so it will work on/under a network and be callable anywhere...
1
1288
by: Diffident | last post by:
Hello Guys, I have an issue with one of the EDP's (Exam Delivery Providers) for Microsoft Certification exams. Someone on this group has suggested me to open a support incident to voice my concern to Microsoft. Can anyone please give me directions on how I can open a support incident? Directions or any pointers will be appreciated. By the way is this service free to public?
6
4644
by: Martin Heuckeroth | last post by:
Hi, We are looking for a way to determine the x and y points of the cursor in a richtext box. We made an VB.NET application with a couple of listboxes and one of them is a richtextlistbox. After a refresh the cursor of the richtextlistbox is reset and goes to top. And that's not a cool thing when you are reading at the bottom of that box and have to look up the point where your were reading manual to have it reset after another
2
6746
by: Don Isgitt | last post by:
Environment: Server running Redhat 3.2.3-20 on quad Xeon 2.4 Postgresql 7.4 compiled from source (gcc 3.2.3) Application written in Perl (5.8.0) using Tk, DBI and DBD Client accessing DB using ODBC (7.03.02) from Win XP box Sample of errors follows: 2004-04-26 10:15:38 LOG: 00000: connection authorized: user=djisgitt database=gds2
7
5914
by: alessandro menchini | last post by:
Hello, First of all: i apologize for my english...i'm still learning... If i create a view it seems that DB2 manage this view like an updateable view. But I need to create a view "read only", so DB2 never enables lock on any record... How can i do that?
1
983
gbpackersfan1122
by: gbpackersfan1122 | last post by:
Hello Everyone, I need help on a code for making a investment calculator. it needs to have a monthly investment, interest rate, and length of investment in the code. the answer is displayed in a list box. thanks in advance for any help!
9
4611
by: shapper | last post by:
Hello, I am displaying a poll using CSS and lists: http://www.27lamps.com/Beta/Poll/Poll.html At the moment I have two problems: 1. I am not able to set a padding between each Bar and the border which I am using background-image to define it.
0
9404
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10164
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9959
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9835
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7379
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3926
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.