I did warn you it was untested
This is the one that works:
Code:
<script type="text/javascript">
String.prototype.isDMString = function() {
var a;
return (
/^\d{2}\/\d{2}$/.test(this) &&
(a = this.match(/(\d{2})\/(\d{2})/))[1] * a[2] > 0 &&
a[1] <= 31 &&
a[2] <= 12
);
};
Number.prototype.pad = function(len) {
var n = this.toString();
while(n.length < len)
n = "0" + n;
return n;
};
function Calendar() {
if(!arguments[0].isDMString) return;
for(var i = 0; i < arguments.length; ++i)
if(arguments[i].isDMString()) {
currDate = arguments[i];
if(!this[currDate]) this[currDate] = [];
} else {
this.addEvent(currDate, arguments[i]);
}
}
Calendar.prototype.addEvent = function(dte, event) {
if(typeof dte !== 'string' || !dte.isDMString())
return false;
if(!this[dte]) this[dte] = [];
this[dte].push(event);
return true;
};
Calendar.prototype.getTodaysEvents = function() {
var now;
return (this[(now = new Date()).getDate().pad(2) + "/" + (now.getMonth() + 1).pad(2)] || []);
};
Calendar.prototype.toString = function() {
var op = "";
for(var i in this) {
if(!i.isDMString()) continue;
op += "\n" + i + ": " + this[i].join(", ");
}
return op;
};
// NOTE: DD/MM format. I'm a Brit. :-P
var cal = new Calendar(
"02/01", "Happy Birthday to John",
"06/01", "Happy Birthday to Max", "Happy Birthday to Jill",
"12/01", "Happy Birthday to Trisha",
"22/01", "Happy Birthday to Hannah",
"26/01", "Happy Birthday to Miriam",
"30/01", "Tomorrow is the last day to submit your assignment."
);
document.write(cal.getTodaysEvents().join("<br>"));
</script>
Another thing that I would like the script to do is to enable me to decide the color and type of fonts used in the birthday greeting. For the guys, I want to use Times Roman in black and for girls, I want to use tahoma or arial in blue.
You can include <span> or (shudder) <font> tags just as you would in plain HTML. Just remember to escape quotes; for example,
Code:
<span style="color:blue;">Happy Birthday to John</span>
becomes:
Code:
"<span style=\"color:blue;\">Happy Birthday to John</span>"
Bookmarks