Main Menu

Contact Andrew Quinn

jaquinn@ihug.co.nz http://twitter.com/jaquinn
What is on next Wednesday......
Written by Administrator   
Friday, 05 December 2008 05:40

Sometimes a simple programming exercise can be a lot of fun and really help someone out.

David (ZL1DK.... the guy who manages the Papakura Radio Club website http://www.qsl.net/zl1vk) asked me the other week if there was any way to have the banner in the top corner that advertised the meeting for the upcoming Wednesday evening to update automatically.  He had done the banner using animated GIF's and was changing the file manually.

Ideally I would have done this as a server side process but the free www.qsl.net hosting doesn't allow such flexibility.... so javascript for the browser provided the solution instead.  Not something I write a lot of but quite satisfying to get a good result.

For anyone who cares here is the script that solved the problem....


// Work out which happening this week message to display

var currentTime = new Date();
var day = currentTime.getDate();
var dow = currentTime.getDay();
var hour = currentTime.getHours();

// Offset to the closest Wednesday

var wed_adj = 3-dow;
var next_meeting = new Date(currentTime);

if (wed_adj < 0)
{
 // Passed Wednesday this week so next meeting on the following Wednesday

 next_meeting.setDate(next_meeting.getDate() + wed_adj + 7);

}
else if (wed_adj == 0 && hour < 20)
{
 // Wednesday and before 2000 hours so next meeting is still today
 

}
else if (wed_adj == 0 && hour >= 20)
{
 // Wednesday and after 2000 hours so next meeting is next week

 next_meeting.setDate(next_meeting.getDate() + 7);

}
else
{
 // Before Wednesday so next meeting is Wednesday this week

 next_meeting.setDate(next_meeting.getDate() + wed_adj);

}

var weekno = 0;

// If the month of the next meeting is not the same as the current month then week 1

if (currentTime.getMonth() != next_meeting.getMonth())
{
    weekno = 1;
}
else
{
    // From the next meeting work out how many previous Wednesdays there are in the month

    var test_meeting = new Date;
    test_meeting.setTime(next_meeting.valueOf());
    while (test_meeting.getMonth() == next_meeting.getMonth())
    {
        weekno++;
        test_meeting.setDate(test_meeting.getDate() - 7);
    }
}  
document.write("\"What'shttp://www.qsl.net/zl1vk/PRCMulti/Banner" + weekno + ".gif\">");