Friday, December 9, 2005

Javascript Date Since Calculator

This is a simple piece of code to calculate the number of days since an event:




function daysSince(yr, mo, dy) {
var anniv_date = new Date(yr, mo-1, dy);
var today = new Date();
var diff = (today - anniv_date)/1000/60/60/24;
document.write(parseInt(diff));
}


You call this within your HTML as daysSince(2001, 09, 30) where the parameters are the year, month, and day of the anniversary.



You can see an example of this at http://timallen.org/blog/staticpages/index.php?page=days_since

No comments:

Post a Comment

I moderate comments blog posts over 14 days old. This keeps a lot of spam away. I generally am all right about moderating. Thanks for understanding.