Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Jan 2005
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    help comparing an array of dates

    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:
    Code:
     
    <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)

  • #2
    Smokes a Lot
    Join Date
    Jul 2003
    Location
    CA, USA
    Posts
    1,594
    Thanks
    5
    Thanked 20 Times in 20 Posts
    See if this works out for you:

    Code:
    <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
    Helping to build a bigger box. - Adam Matthews

  • #3
    New to the CF scene
    Join Date
    Jan 2005
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #4
    Smokes a Lot
    Join Date
    Jul 2003
    Location
    CA, USA
    Posts
    1,594
    Thanks
    5
    Thanked 20 Times in 20 Posts
    Oh I see. Try it like this.

    Code:
    <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
    Helping to build a bigger box. - Adam Matthews

  • #5
    New to the CF scene
    Join Date
    Jan 2005
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Perfect! Works great! Thanks a lot- both for your expertise and quick responses.

  • #6
    New to the CF scene
    Join Date
    Jan 2005
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #7
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,037
    Thanks
    0
    Thanked 250 Times in 246 Posts
    I haven't really checked the code but try changing getYear() to getFullYear()

  • #8
    New to the CF scene
    Join Date
    Jan 2005
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you Basscyst and glenngv! Works perfectly!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •