prøv denne...
<div id="test"></div>
<script type="text/javascript">
if(!Date.prototype.getDaysInMonth) {
// private
Date.daysInMonth = [31,28,31,30,31,30,31,31,30,31,30,31];
// public
Date.prototype.getDaysInMonth = function() {
Date.daysInMonth[1] = this.isLeapYear() ? 29 : 28;
return Date.daysInMonth[this.getMonth()];
};
}
if(!Date.prototype.isLeapYear) {
// private
Date.prototype.isLeapYear = function() {
var year = this.getFullYear();
return !!((year & 3) == 0 && (year % 100 || (year % 400 == 0 && year)));
}
}
/**
* @class Countdown
* @param {string/HTMLElement} el target HTMLElement or id of target HTMLElement.
* @param {string/Date Object} time target time to countdown to.
*/
var CountDown = function(el, time) {
if(!el.innerHTML) {
el = document.getElementById(el);
}
this.el = el;
if(typeof time != "object" || !time.getTime) {
time = new Date(time);
}
this.target = time;
}
CountDown.prototype = {
dayText : 'day',
monthText : 'month',
yearText : 'year',
hourText : 'hour',
minuteText : 'minute',
secondText : 'second',
plural : 's',
/**
* Gets distance in seconds
* @return {number}
*/
getDist : function() {
var now = new Date();
var dist = now.getTime() - this.target.getTime();
return Math.abs(dist);
},
/**
* Gets calculated distance
* @return {object}
*/
getCount : function() {
var dist = this.getDist();
var second = 1000
var minute = 60 * second;
var hour = 60 * minute;
var day = 24 * hour;
var days = Math.floor(dist / day);
var hours = Math.floor((dist - (days*day)) / hour);
var minutes = Math.floor((dist - (days*day+hours*hour)) / minute);
var seconds = Math.floor((dist - (days*day+hours*hour+minutes*minute)) / second);
var tmpTime = new Date();
var tmpDays = 0, retractionDays = 0;
var months = 0;
while(days > tmpDays) {
tmpTime.setMonth(tmpTime.getMonth()+1);
tmpDays += tmpTime.getDaysInMonth();
if(days < retractionDays + tmpTime.getDaysInMonth()) {
} else {
retractionDays += tmpTime.getDaysInMonth();
months++;
}
}
var years = Math.floor(months / 12);
if(years > 0) {
months = months - years * 12;
}
return {
"years" : years, "months" : months, "days" : days-retractionDays, "hours" : hours, "minutes" : minutes, "seconds" : seconds
}
},
/**
* starts countdown
*/
start : function() {
var c = this.getCount()
var txt = [
c.years + ' ' + this.yearText+(c.years != 1 ? this.plural : '')+ ' ',
c.months + ' ' + this.monthText+(c.months != 1 ? this.plural : '')+ ' ',
c.days + ' ' + this.dayText+(c.days != 1 ? this.plural : '')+ ' ',
c.hours + ' ' + this.hourText+(c.hours != 1 ? this.plural : '')+ ' ',
c.minutes + ' ' + this.minuteText+(c.minutes != 1 ? this.plural : '')+ ' ',
c.seconds + ' ' + this.secondText+(c.seconds != 1 ? this.plural : '')+ ' '
]
this.el.innerHTML = txt.join('');
var fn = this._delegate(this.start,this);
this.timer = setTimeout(fn,1000);
},
/**
* stops countdown
*/
stop : function() {
clearTimeout(this.timer);
},
// private
_delegate : function(func,obj) {
return function() {
return func.apply(obj,[]);
}
}
}
window.onload = function() {
// Start the countdown
var cd = new CountDown("test", new Date("Jun 01 2009 16:00:00"));
cd.start();
}
</script>