...

View Full Version : help comparing an array of dates



marvinDmartian
01-19-2005, 06:00 PM
I'm working on a site for a non-profit and am a novice with javascript. The site host they use does not allow server-side access, so js and html is my only option.

What I'm trying to do is write a code to display a message for the next meeting date of a semi-regular gathering. Since I only work at this part time, I'd like to add the meeting dates and messages up to 6 months or so in advance.

My idea was to create 2 arrays- one with the dates and one with the messages- both in chronological order. The JS would use a 'while' loop to compare the dates in the array to the current date, and when the current date was no longer larger than the array date, the message corresponding to the last date would be displayed.

Unfortunately, my code doesn't work- nothing happens. Here's what I have so far:


<head>
<script>
function dateMsg() {
var days= new Array();
var msgs= new Array();

days[0] = "05,0,1";
msgs[0] = "january 1st message";

days[1] = "05,0,10";
msgs[1] = "january 10th message";

days[2] = "05,0,20";
msgs[2] = "january 20th message";

days[3] = "05,0,30";
msgs[3] = "january 30th message";

days[4] = "05,1,1";
msgs[4] = "february 1st message";

days[5] = "05,1,10";
msgs[5] = "february 10th message";

i = 0;
var today = new date();
var expire = new Date(days[i]);

while (today.getTime() > expire.getTime()) {
i++;
}
document.write(msgs[i]);
}
</script>
</head>

<BODY>

<script>
dateMsg()
</script>

</BODY>


Please let me know where I've gone astray (or if there's an easier way). I'm terribly afraid it's some basic concept I've overlooked or don't fully grasp.

Thanks in advance (and apologies if I've breached some forum protocol)

Basscyst
01-19-2005, 07:04 PM
See if this works out for you:



<script type="text/javascript">

var msg=new Array();
msg["d1_1_2005"]="msg1";
msg["d1_2_2005"]="msg2";
msg["d1_3_2005"]="msg3";
msg["d1_4_2005"]="msg4";
msg["d1_5_2005"]="msg5";
msg["d1_6_2005"]="msg6";
msg["d1_19_2005"]="msg7";

var now=new Date();
var dt=now.getDate();
var mo=now.getMonth()+1;
var yr=now.getYear();
var ndate='d'+mo+'_'+dt+'_'+yr;

var str=msg[ndate]
alert(str);

</script>


Basscyst

marvinDmartian
01-19-2005, 07:29 PM
Thanks, but I don't think it does what I need. In this example, if the current day is not the same day as an event, nothing will show up.

I need the message for next date to be displayed- eg. if it's jan. 19 and the array has messages for the jan. 16, jan. 21, and jan. 28, the message for jan. 21st should be displayed.

Any ideas?

Basscyst
01-19-2005, 08:05 PM
Oh I see. Try it like this.




<script type="text/javascript">

var msg=new Array();
msg["d1_1_2005"]="msg1";
msg["d1_2_2005"]="msg2";
msg["d1_3_2005"]="msg3";
msg["d1_4_2005"]="msg4";
msg["d1_5_2005"]="msg5";
msg["d1_6_2005"]="msg6";
msg["d1_18_2005"]="no";
msg["d1_21_2005"]="msg7";

var now=new Date();
var dt=now.getDate();
var mo=now.getMonth()+1;
var yr=now.getYear();
var ndate=mo+'/'+dt+'/'+yr;
getNextDate(ndate);
function getNextDate(d)
{
var now=new Date(d);
var dt=now.getDate();
var mo=now.getMonth()+1;
var yr=now.getYear();
var ndate=mo+'/'+dt+'/'+yr;
var x=ndate.replace("/","_")
var x=x.replace("/","_")
x="d"+x;
if(undefined==msg[x])
{
dt=dt+1;
ndate=mo+'/'+dt+'/'+yr;
getNextDate(ndate);
}
else
{
str=msg[x]
}
}

alert(str);

</script>



Basscyst

marvinDmartian
01-20-2005, 02:05 AM
Perfect! Works great! Thanks a lot- both for your expertise and quick responses.

marvinDmartian
01-24-2005, 11:39 PM
One last question- this doesn't seem to work in NS or FF. Most users to the site use IE (97% at last check) so this isn't extremely critical, but is there a way to change it to accomodate the remaining 3%?

Thanks

glenngv
01-25-2005, 03:18 AM
I haven't really checked the code but try changing getYear() to getFullYear()

marvinDmartian
01-25-2005, 04:41 PM
Thank you Basscyst and glenngv! Works perfectly! :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum