Tương tự như hiệu ứng Số ngày còn lại nhưng đoạn mã JavaScript JavaScript này sẽ thông báo cho bạn nếu sự kiện bạn cài đặt đã qua rồi.
JavaScript:
<script language="javascript">
<!--
/*
Created by: James Crooke :: http://www.cj-design.com
** Additional formats can be seen on the Web site at:
http://www.cj-design.com/demos/cjeventcountdownv1/ */
function renderMessage(dateStr, msg1, msg2, countFrom) {
var date = new Date(dateStr);
var now = new Date();
var diff = date.getTime() - now.getTime();
var days = Math.floor(diff / (1000 * 60 * 60 * 24)) + 1;
if(days < 1) {
document.write(msg1);
} else {
if(countFrom)
days = countFrom - days;
document.write(msg2.replace(/%days%/g, number_format(days)));
}
}
function number_format(num) {
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num)) {
num = "0";
}
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
num = Math.floor(num/100).toString();
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+','+
num.substring(num.length-(4*i+3));
return (((sign)?'':'-') + num);
}
// -->
</script>
HTML:
<script type="text/javascript">
<!--
// 1st parameter: "target date", 2nd parameter: "late message", 3rd parameter: "early message".
renderMessage("December 11, 2008", "You missed it!!", "Hurry, there's only %days% days to go!");
//-->
</script>
</blockquote>
<p>
In this example, the date is set to <strong>November 15, 2005</strong>, a date
which has passed...</p>
<blockquote>
<script type="text/javascript">
<!--
renderMessage("November 15, 2005", "You missed it!", "Hurry, there's only %days% days to go!");
//-->
</script>