Как
узнать, сколько дней осталось до часа Икс?
Например сколько дней осталось до Нового
ГОДА 2009!!!
(текст зелёным цветом необходимо изменить,
как вам угодно).
Код.
<script type="text/javascript">
<!--
function getDaysLeft(oDeadLineDate, oToday){
return oDeadLineDate > oToday ? Math.ceil((oDeadLineDate -
oToday) / (1000 * 60 * 60 * 24)) : null;
}
function MkDaysLeft(sDeadLineDate, sDeadLineText){
var oToday = new Date();
var sTime = sDeadLineDate + " " + oToday.getHours() + ":" +
oToday.getMinutes() + ":" + oToday.getSeconds();
var oDeadLineDate = new Date(sTime);
var nDaysLeft = getDaysLeft(oDeadLineDate, oToday);
if (nDaysLeft){
var sDaysLeft = String(nDaysLeft);
var sDaysText = "дней";
var nDaysLeftLength = sDaysLeft.length;
if (sDaysLeft.charAt(nDaysLeftLength - 2) != "1"){
if (sDaysLeft.charAt(nDaysLeftLength - 1) == "2" ||
sDaysLeft.charAt(nDaysLeftLength - 1) == "3" ||
sDaysLeft.charAt(nDaysLeftLength - 1) == "4"){
sDaysText = "дня";
}else if (sDaysLeft.charAt(nDaysLeftLength - 1) == "1"){
sDaysText = "день";
}
}
var sLeftText = sDaysText == "день"
? "остался" : "осталось";
document.write(sDeadLineText + " " + sLeftText + " " +
nDaysLeft + " " + sDaysText + ".");
}else{
document.write("С
НОВЫМ 2009 ГОДОМ!!!");
}
}
MkDaysLeft("January
01, 2009",
"До 2009 года
");
//-->
</script>
Результат.
|