Người dùng một vào một thời điểm">thời điểm và hiệu ứng sẽ tính xem từ thời điểm đó đến hiện tại đã được bao nhiêu ngày.
JavaScript:
<script language="JavaScript">
// Paul Hartmann (guycroft@clara.co.uk) | http://www.guycroft.clara.net/
<!-- Begin
function howLong(yr, mo, dy) { //Parameters are calendar Year,Month,Day
var moFlag = 0;
var dyFlag = 0;
var dy = frm.day.value;
var mo = frm.month.value;
var yr = frm.year.value;
var nDate = new Date(); // current date (local)
var nowTime = nDate.getTime(); // current time (UTC)
var thenTime = Date.UTC(yr, mo-1, dy); // specified time (UTC)
var thisYear = nDate.getFullYear();
var thisMonth = nDate.getMonth();
var thisDay = nDate.getDate();
if (isNaN(dy) || isNaN(mo) || isNaN(yr)) {
window.alert("That is not a valid date. Numbers only, please.");
frm.day.focus();
return false
}
if (dy<1 || dy>31) {
window.alert("Day "+dy+" of Month "+mo+" is not a valid date");
frm.day.focus();
return false
} else {
dyFlag=1;
}
if (dy>30 && (mo == 2 || mo==4 || mo==6 || mo==9 || mo==11)) {
window.alert("Day "+dy+" of Month "+mo+" is not a valid date");
frm.day.focus();
return false
} else {
dyFlag=1;
}
if (dy>29 && mo==2) {
window.alert("Day "+dy+" of Month "+mo+" is not a valid date");
frm.day.focus();
return false
} else {
dyFlag=1;
}
if ((mo == 2 && dy == 29) && ((yr%4 != 0) || (yr%100 == 0 && yr%400 != 0))) {
window.alert("29th of February is not a valid date in "+yr);
frm.day.focus();
return false
} else {
dyFlag=1;
}
if (mo<1 || mo>12) {
window.alert("Day "+dy+" of Month "+mo+" is not a valid date");
frm.month.focus();
return false
} else {
moFlag=1;
}
if (dyFlag==1 && moFlag==1) {
if (nowTime >= thenTime) { //-----------------Past or present time
if ((thisMonth > mo-1) || ((thisMonth == mo-1) && (thisDay >= dy))) {
whYrs = thisYear - yr;
spareDys = parseInt((nowTime - Date.UTC(thisYear,mo-1,dy))/(3600000*24));
if ((mo == 2 && dy == 29) && ((thisYear%4 != 0) || (thisYear%100 == 0 && thisYear%400 != 0))) {spareDys = spareDys + 1}
} else {
whYrs = thisYear - yr - 1;
spareDys = parseInt((nowTime - Date.UTC(thisYear-1,mo-1,dy))/(3600000*24));
if ((mo == 2 && dy == 29) && (((thisYear-1)%4 != 0) || ((thisYear-1)%100 == 0 && (thisYear-1)%400 != 0))) {spareDys = spareDys + 1}
}
} else { //----------------------------Future time
if ((thisMonth < mo-1) || ((thisMonth == mo-1)&& (thisDay <= dy))) {
whYrs = yr - thisYear;
spareDys = parseInt((thenTime - Date.UTC(yr,thisMonth,thisDay))/(3600000*24));
if ((thisMonth == 1 && thisDay == 29) && ((yr%4 != 0) || (yr%100 == 0 && yr%400 != 0))) {spareDys = spareDys - 1}
} else {
whYrs = yr - thisYear - 1;
spareDys = parseInt((thenTime - Date.UTC(yr-1,thisMonth,thisDay)) /(3600000*24));
if ((thisMonth == 1 && thisDay == 29) && (((yr-1)%4 != 0) || ((yr-1)%100 == 0 && (yr-1)%400 != 0))) {spareDys = spareDys - 1};
}
}
}
if (nowTime >= thenTime) {
frm.timeBetween.value = "It is "+whYrs +
(whYrs == 1 ? " Year and " : " Years and ") + spareDys +
(spareDys == 1 ? " Day " : " Days ") + "since then"
} else {
frm.timeBetween.value = "It is "+whYrs +
(whYrs == 1 ? " Year and " : " Years and ") + spareDys +
(spareDys == 1 ? " Day " : " Days ") + "until then"
}
}
// End -->
</script>
<body onload="frm.day.focus()">
<form name="frm" action="GET" onreset="frm.day.focus()">
Enter a date � <b>Day, Month and Year</b> � in numbers:<br>
(January=01, ... December=12)<br>
dd mm yyyy<br>
<input type="text" name="day" value="" maxlength="2" size="2">
<input type="text" name="month" value="" maxlength="2" size="2">
<input type="text" name="year" value="" maxlength="4" size="4">
<input type="button" name="Calc" value="Calculate How Long" onclick="howLong(year,month,day)">
<input type="text" name="timeBetween" size="40">
<input type="reset" value="Clear">