canada4ever
11-08-2006, 03:12 AM
Hi there. I'm a complete newbie to javascript but I managed to edit a birthday someone had posted here to show events I put into the array. Now the thing is is that I would like to be able to set how many events are shown. So instead of it displaying all the events for the next week it would only display the next 4 events.
Here's the code
<script type="text/javascript">
var arrEvents = [
['Aunt B.', '11/5'],
['Goober', '11/7'],
['Andy', '11/7'],
['Otis', '11/8'],
['Earnest T.', '11/9'],
['Opey', '11/9'],
['Barney', '11/8'],
['Thelma Lu', '5/13']
//...and so on (last entry must not have a trailing comma)
];
function getEventsThisWeek(){
var arrMonth = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
var events, idx;
var eventsList = new Array();
var today = new Date();
for (var i=0;i<arrEvents.length;i++){
var events = new Date(arrEvents[i][1] + '/' + today.getFullYear());
if (isNaN(events)) continue;
if ( isEventsInRange(events, 3) ){ <!-- How many days to show birthday's to. -->
idx = eventsList.length;
eventsList[idx] = new Object();
eventsList[idx].name = arrEvents[i][0];
eventsList[idx].events = events;
eventsList[idx].month = arrMonth[events.getMonth()];
}
}
if (eventsList.length > 0){ //sort asc by birthdate
eventsList.sort(
function(a, b){
if (a.events < b.events) return -1
if (a.events > b.events) return 1;
return 0;
}
);
}
return eventsList;
}
function isEventsInRange(events, interval){
var today = new Date();
//have to override time so entire day will be valid
today.setHours(0,0,0,0);
//if the birthday has already occurred in the year, increment to the next year
if (events < today)
events.setFullYear(events.getFullYear() + 1);
// get ms between dates (UTC) and make into "difference" date
var iDiffMS = events.valueOf() - today.valueOf();
//divide iDiffMS by 1000, Seconds, Minutes, Hours
nDays = parseInt(iDiffMS / 1000 / 60 / 60 / 24);
if(parseInt(nDays) <= parseInt(interval))
return true;
else
return false;
}
function displayEventsList(){
var date = new Date().getDate();
var eventsList = getEventsThisWeek();
var len = eventsList.length;
var s = "<h4>Happy Birthday to...</h4>";
if (len>0){
s += '<ul>';
for (var i=0; i<len; i++){
//be mindful of the string-line continuation character (\) at the end of the first line
s += '<li' + ((date == eventsList[i].events.getDate())?' class="eventsToday"':'')+ '>\
<strong>' + eventsList[i].name + '</strong> - '
+ eventsList[i].month + ' ' + eventsList[i].events.getDate() + '</li>';
}
s += '</ul>';
}
else{
s += "No Event's This Week.";
}
document.write(s);
}
</script>
Any help would be appreciated.
Thanks.
Matthew.
Here's the code
<script type="text/javascript">
var arrEvents = [
['Aunt B.', '11/5'],
['Goober', '11/7'],
['Andy', '11/7'],
['Otis', '11/8'],
['Earnest T.', '11/9'],
['Opey', '11/9'],
['Barney', '11/8'],
['Thelma Lu', '5/13']
//...and so on (last entry must not have a trailing comma)
];
function getEventsThisWeek(){
var arrMonth = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
var events, idx;
var eventsList = new Array();
var today = new Date();
for (var i=0;i<arrEvents.length;i++){
var events = new Date(arrEvents[i][1] + '/' + today.getFullYear());
if (isNaN(events)) continue;
if ( isEventsInRange(events, 3) ){ <!-- How many days to show birthday's to. -->
idx = eventsList.length;
eventsList[idx] = new Object();
eventsList[idx].name = arrEvents[i][0];
eventsList[idx].events = events;
eventsList[idx].month = arrMonth[events.getMonth()];
}
}
if (eventsList.length > 0){ //sort asc by birthdate
eventsList.sort(
function(a, b){
if (a.events < b.events) return -1
if (a.events > b.events) return 1;
return 0;
}
);
}
return eventsList;
}
function isEventsInRange(events, interval){
var today = new Date();
//have to override time so entire day will be valid
today.setHours(0,0,0,0);
//if the birthday has already occurred in the year, increment to the next year
if (events < today)
events.setFullYear(events.getFullYear() + 1);
// get ms between dates (UTC) and make into "difference" date
var iDiffMS = events.valueOf() - today.valueOf();
//divide iDiffMS by 1000, Seconds, Minutes, Hours
nDays = parseInt(iDiffMS / 1000 / 60 / 60 / 24);
if(parseInt(nDays) <= parseInt(interval))
return true;
else
return false;
}
function displayEventsList(){
var date = new Date().getDate();
var eventsList = getEventsThisWeek();
var len = eventsList.length;
var s = "<h4>Happy Birthday to...</h4>";
if (len>0){
s += '<ul>';
for (var i=0; i<len; i++){
//be mindful of the string-line continuation character (\) at the end of the first line
s += '<li' + ((date == eventsList[i].events.getDate())?' class="eventsToday"':'')+ '>\
<strong>' + eventsList[i].name + '</strong> - '
+ eventsList[i].month + ' ' + eventsList[i].events.getDate() + '</li>';
}
s += '</ul>';
}
else{
s += "No Event's This Week.";
}
document.write(s);
}
</script>
Any help would be appreciated.
Thanks.
Matthew.