以前收集的一些资料---JS中处理日期的一些函数和方法
batman 发表于 99-8-16 22:51:09 Joy ASP ←返回版面 JavaScript日期处理函数大全
其实还很不全,呵呵,比起VBS来还是少点的。但是考虑到
有人经常问到有关如何使用JS处理日期的问题,我只好献拙了。
希望能够有点用处。
1。解决2000问题
function y2k(number) { return (number < 1000) ? number + 1900 : number; }
2。检查日期是否合法
// 当输入参数为isDate(dd,mm,ccyy)时,表示要检查年,月,日
// 当输入参数为isDate(dd,mm) 表示默认年为当前年
// 当输入参数为isDate(dd) 表示默认年,月为当前年月
// 注意输入月份保证在1-12以内。function isDate (day,month,year) {
var today = new Date();
year = ((!year) ? y2k(today.getYear())year);
month = ((!month) ? today.getMonth():month-1);
if (!day) return false
var test = new Date(year,month,day);
if ( (y2k(test.getYear()) == year) &&
(month == test.getMonth()) &&
(day == test.getDate()) )
return true;
else
return false
}以下是调用例子:
if (isDate(31,2,1997))
document.write("Valid");
else
document.write("Invalid");3。如何判断两个日期中的间隔天数
function daysElapsed(date1,date2) {
var difference = Date.UTC(date1.getYear(),date1.getMonth(),date1.getDate(),0,0,0)
- Date.UTC(date2.getYear(),date2.getMonth(),date2.getDate(),0,0,0);
return difference/1000/60/60/24;
}4。如何将一个下拉列表框中的月份传递到另一页
<FORM>
<SELECT NAME="selectName">
<OPTION>January
<OPTION>February
<OPTION>March
<OPTION>April
<OPTION>May
<OPTION>June
<OPTION>July
<OPTION>August
<OPTION>Spetember
<OPTION>October
<OPTION>November
<OPTION>December
</SELECT>
<INPUT TYPE="BUTTON" VALUE="Go" onClick="window.location.href = 'nextpage.html?' +
this.form.selectName.options[this.form.selectName.selectedIndex].text">
</FORM>在nextpage.html中加入下面的代码
<FORM name="formName"><INPUT TYPE="TEXT" name="textName"><FORM>
<SCRIPT LANGUAGE="JavaScript"><
document.formName.textName.value = location.search.substring(1);
//-SCRIPT>或则:
<SCRIPT LANGUAGE="JavaScript"><
document.write("<FORM><INPUT TYPE='TEXT' ");
document.write("VALUE='"location.search.substring(1)+"FORM>")
//-SCRIPT>5。如何将一个字符串中的时间和当前时间做比较
<SCRIPT LANGUAGE="JavaScript">
/*其中的日期字符串可有以下格式:
格式 1 : 19970529
格式 2 : 970529
格式 3 : 29/05/1997
格式 4 : 29/05/97
输入参数dateType是1到4的数字,表示使用哪种格式.
*/
<!-
function isitToday(dateString,dateType) {
var now = new Date();
var today = new Date(now.getYear(),now.getMonth(),now.getDate()) if (dateType == 1)
var date = new Date(dateString.substring(0,4),
dateString.substring(4,6)-1,
dateString.substring(6,8));
else if (dateType == 2)
var date = new Date(dateString.substring(0,2),
dateString.substring(2,4)-1,
dateString.substring(4,6));
else if (dateType == 3)
var date = new Date(dateString.substring(6,10),
dateString.substring(3,5)-1,
dateString.substring(0,2));
else if (dateType == 4)
var date = new Date(dateString.substring(6,8),
dateString.substring(3,5)-1,
dateString.substring(0,2));
else
return false; if (date.toString() == today.toString())
return true;
else
return false;
}
调用的例子如下:
if (isitToday("19970529",1)) alert('true'); else alert('false');
if (isitToday("970529",2)) alert('true'); else alert('false');
if (isitToday("29/05/1997",3)) alert('true'); else alert('false');
if (isitToday("02/06/97",4)) alert('true'); else alert('false');
//-
</SCRIPT>6。如何根据一个人的生日计算他的岁数
<SCRIPT LANGUAGE="JavaScript"><
/*其中的日期字符串可有以下格式:
格式 1 : 19970529
格式 2 : 970529
格式 3 : 29/05/1997
格式 4 : 29/05/97
输入参数dateType是1到4的数字,表示使用哪种格式.
*/
function getAge(dateString,dateType) {
var now = new Date();
var today = new Date(now.getYear(),now.getMonth(),now.getDate())
var yearNow = now.getYear();
var monthNow = now.getMonth();
var dateNow = now.getDate(); if (dateType == 1)
var dob = new Date(dateString.substring(0,4),
dateString.substring(4,6)-1,
dateString.substring(6,8));
else if (dateType == 2)
var dob = new Date(dateString.substring(0,2),
dateString.substring(2,4)-1,
dateString.substring(4,6));
else if (dateType == 3)
var dob = new Date(dateString.substring(6,10),
dateString.substring(3,5)-1,
dateString.substring(0,2));
else if (dateType == 4)
var dob = new Date(dateString.substring(6,8),
dateString.substring(3,5)-1,
dateString.substring(0,2));
else
return ''; var yearDob = dob.getYear();
var monthDob = dob.getMonth();
var dateDob = dob.getDate(); yearAge = yearNow - yearDob; if (monthNow > monthDob)
var monthAge = monthNow - monthDob;
else {
yearAge--;
var monthAge = 12 + monthNow -monthDob;
}
if (dateNow > dateDob)
var dateAge = dateNow - dateDob;
else {
monthAge--;
var dateAge = 31 + dateNow - dateDob;
} return yearAge + ' years ' + monthAge + ' months ' + dateAge + ' days';
}
调用例子
document.write(getAge("19650104",1)+'BR>')
document.write(getAge("650104",2)+'BR>')
document.write(getAge("04/01/1965",3)+'BR>')
document.write(getAge("04/01/65",4)+'BR>')
//-SCRIPT>7。如何使用下面的格式dd/mm/yy在网页中显示日期
<SCRIPT LANGUAGE = 'JavaScript'>
<!-
var date = new Date();
var d = date.getDate();
var day = (d < 10) ? '0' + d : d;
var m = date.getMonth() + 1;
var month = (m < 10) ? '0' + m : m;
var yy = date.getYear();
var year = (yy < 1000) ? yy + 1900 : yy;document.write(day + "/" + month + "/" + year);
//-
</SCRIPT>8。如何使用下面的格式date month year在网页中显示日期
<SCRIPT LANGUAGE = 'JavaScript'>
<!-
function makeArray() {
for (i = 0; i<makeArray.arguments.length; i++)
this[i + 1] = makeArray.arguments[i];
}var months = new makeArray('January','February','March',
'April','May','June','July','August','September',
'October','November','December');var date = new Date();
var day = date.getDate();
var month = date.getMonth() + 1;
var yy = date.getYear();
var year = (yy < 1000) ? yy + 1900 : yy;document.write(day + " " + months[month] + " " + year);
//-
</SCRIPT>9.如何让我的网页的最近更新日期更易读
<SCRIPT LANGUAGE = 'JavaScript'><
function makeArray0() {
for (i = 0; i<makeArray0.arguments.length; i++)
this[i] = makeArray0.arguments[i];
}
var days = new makeArray0("Sunday","Monday","Tuesday","Wednesday",
"Thursday","Friday","Saturday");
var months = new makeArray0('January','February','March',
'April','May','June','July','August','September',
'October','November','December');
function nths(day) {
if (day == 1 || day == 21 || day == 31)
return 'st';
else
if (day == 2 || day == 22)
return 'nd';
if (day == 3 || day == 23)
return 'rd';
else return 'th';
}
function y2k(number) { return (number < 1000) ? number + 1900 : number; }
var last = document.lastModified;
var date = new Date(last);
document.write("Last updated on " + days[date.getDay()] + ' ' +
date.getDate() + nths(date.getDate()) + " " +
months[date.getMonth()] + ", " +
(y2k(date.getYear()) + "."
//-SCRIPT>10。如何显示到某个特定日期的倒记时
<SCRIPT LANGUAGE="JavaScript"><
function y2k(number) { return (number < 1000) ? number + 1900 : number; }function timeTillDate(whenDay,whenMonth,whenYear) {
var now = new Date();
var thisDay = now.getDate(), thisMonth = now.getMonth() + 1, thisYear = y2k(now.getYear())
var yearsDifference = whenYear - thisYear, monthsDifference = 0, daysDifference = 0, string = ''; if (whenMonth >= thisMonth) monthsDifference = whenMonth - thisMonth;
else { yearsDifference--; monthsDifference = whenMonth + 12 - thisMonth; } if (whenDay >= thisDay)daysDifference = whenDay - thisDay;
else {
if (monthsDifference > 0) monthsDifference--;
else { yearsDifference--; monthsDifference+=11; }
daysDifference = whenDay + 31 - thisDay;
} if (yearsDifference < 0) return ''; if ((yearsDifference == 0) && (monthsDifference == 0) && (daysDifference == 0))
return ''; if (yearsDifference > 0) {
string = yearsDifference + ' year';
if (yearsDifference > 1) string += 's';
string += ' ';
} if (monthsDifference > 0) {
string += monthsDifference + ' month';
if (monthsDifference > 1) string += 's';
string += ' ';
}
if (daysDifference > 0) {
string += daysDifference + ' day';
if (daysDifference > 1) string += 's';
string += ' ';
} var difference = Date.UTC(now.getYear(),now.getMonth(),now.getDate(),now.getHours(),now.getMinutes(),now.getSeconds()) -
Date.UTC(now.getYear(),now.getMonth(),now.getDate(),0,0,0); difference = 1000*60*60*24 - difference; var hoursDifference = Math.floor(difference/1000/60/60);
difference = difference - hoursDifference*1000*60*60
var minutesDifference = Math.floor(difference/1000/60);
difference = difference - minutesDifference*1000*60
var secondsDifference = Math.floor(difference/1000); if (hoursDifference > 0) {
string += hoursDifference + ' hour';
if (hoursDifference > 1) string +='s';
string += ' ';
} if (minutesDifference > 0) {
string += minutesDifference + ' minute';
if (minutesDifference > 1) string +='s';
string += ' ';
} if (secondsDifference > 0) {
string += secondsDifference + ' second';
if (secondsDifference > 1) string +='s';
string += ' ';
}
return string;
}调用例子,例如现在到31/12/1999还有多久。
document.write(timeTillDate(31,12,1999));
//-SCRIPT>11。如何从一个日期中减掉几个小时
<SCRIPT LANGUAGE = 'JavaScript'><
var date = new Date();
var date = new Date(Date.UTC(date.getYear(),date.getMonth(),date.getDate(),date.getHours(),date.getMinutes(),date.getSeconds()) - 5*60*60*1000);
document.write(date);
//-SCRIPT>12。如何在一个日期中增加几个月后并能够正确显示出来
<SCRIPT LANGUAGE="JavaScript"><
function makeArray() {
for (i = 0; i<makeArray.arguments.length; i++)
this[i + 1] = makeArray.arguments[i];
}
var months = new makeArray('January','February','March','April',
'May','June','July','August','September',
'October','November','December');
function nths(day) {
if (day == 1 || day == 21 || day == 31) return 'st';
else if (day == 2 || day == 22) return 'nd';
else if (day == 3 || day == 23) return 'rd';
else return 'th';
}
function y2k(number) { return (number < 1000) ? number + 1900 : number; }
function monthsahead(noofmonths) {
var today = new Date();
var date = new Date(today.getYear(),today.getMonth() + noofmonths,today.getDate(),today.getHours(),today.getMinutes(),today.getSeconds())
return date.getDate() + nths(date.getDate()) + ' ' + months[date.getMonth() + 1] + ' ' + y2k(date.getYear())
}调用例子:
document.write(monthsahead(6));
//-SCRIPT>
batman 发表于 99-8-16 22:51:09 Joy ASP ←返回版面 JavaScript日期处理函数大全
其实还很不全,呵呵,比起VBS来还是少点的。但是考虑到
有人经常问到有关如何使用JS处理日期的问题,我只好献拙了。
希望能够有点用处。
1。解决2000问题
function y2k(number) { return (number < 1000) ? number + 1900 : number; }
2。检查日期是否合法
// 当输入参数为isDate(dd,mm,ccyy)时,表示要检查年,月,日
// 当输入参数为isDate(dd,mm) 表示默认年为当前年
// 当输入参数为isDate(dd) 表示默认年,月为当前年月
// 注意输入月份保证在1-12以内。function isDate (day,month,year) {
var today = new Date();
year = ((!year) ? y2k(today.getYear())year);
month = ((!month) ? today.getMonth():month-1);
if (!day) return false
var test = new Date(year,month,day);
if ( (y2k(test.getYear()) == year) &&
(month == test.getMonth()) &&
(day == test.getDate()) )
return true;
else
return false
}以下是调用例子:
if (isDate(31,2,1997))
document.write("Valid");
else
document.write("Invalid");3。如何判断两个日期中的间隔天数
function daysElapsed(date1,date2) {
var difference = Date.UTC(date1.getYear(),date1.getMonth(),date1.getDate(),0,0,0)
- Date.UTC(date2.getYear(),date2.getMonth(),date2.getDate(),0,0,0);
return difference/1000/60/60/24;
}4。如何将一个下拉列表框中的月份传递到另一页
<FORM>
<SELECT NAME="selectName">
<OPTION>January
<OPTION>February
<OPTION>March
<OPTION>April
<OPTION>May
<OPTION>June
<OPTION>July
<OPTION>August
<OPTION>Spetember
<OPTION>October
<OPTION>November
<OPTION>December
</SELECT>
<INPUT TYPE="BUTTON" VALUE="Go" onClick="window.location.href = 'nextpage.html?' +
this.form.selectName.options[this.form.selectName.selectedIndex].text">
</FORM>在nextpage.html中加入下面的代码
<FORM name="formName"><INPUT TYPE="TEXT" name="textName"><FORM>
<SCRIPT LANGUAGE="JavaScript"><
document.formName.textName.value = location.search.substring(1);
//-SCRIPT>或则:
<SCRIPT LANGUAGE="JavaScript"><
document.write("<FORM><INPUT TYPE='TEXT' ");
document.write("VALUE='"location.search.substring(1)+"FORM>")
//-SCRIPT>5。如何将一个字符串中的时间和当前时间做比较
<SCRIPT LANGUAGE="JavaScript">
/*其中的日期字符串可有以下格式:
格式 1 : 19970529
格式 2 : 970529
格式 3 : 29/05/1997
格式 4 : 29/05/97
输入参数dateType是1到4的数字,表示使用哪种格式.
*/
<!-
function isitToday(dateString,dateType) {
var now = new Date();
var today = new Date(now.getYear(),now.getMonth(),now.getDate()) if (dateType == 1)
var date = new Date(dateString.substring(0,4),
dateString.substring(4,6)-1,
dateString.substring(6,8));
else if (dateType == 2)
var date = new Date(dateString.substring(0,2),
dateString.substring(2,4)-1,
dateString.substring(4,6));
else if (dateType == 3)
var date = new Date(dateString.substring(6,10),
dateString.substring(3,5)-1,
dateString.substring(0,2));
else if (dateType == 4)
var date = new Date(dateString.substring(6,8),
dateString.substring(3,5)-1,
dateString.substring(0,2));
else
return false; if (date.toString() == today.toString())
return true;
else
return false;
}
调用的例子如下:
if (isitToday("19970529",1)) alert('true'); else alert('false');
if (isitToday("970529",2)) alert('true'); else alert('false');
if (isitToday("29/05/1997",3)) alert('true'); else alert('false');
if (isitToday("02/06/97",4)) alert('true'); else alert('false');
//-
</SCRIPT>6。如何根据一个人的生日计算他的岁数
<SCRIPT LANGUAGE="JavaScript"><
/*其中的日期字符串可有以下格式:
格式 1 : 19970529
格式 2 : 970529
格式 3 : 29/05/1997
格式 4 : 29/05/97
输入参数dateType是1到4的数字,表示使用哪种格式.
*/
function getAge(dateString,dateType) {
var now = new Date();
var today = new Date(now.getYear(),now.getMonth(),now.getDate())
var yearNow = now.getYear();
var monthNow = now.getMonth();
var dateNow = now.getDate(); if (dateType == 1)
var dob = new Date(dateString.substring(0,4),
dateString.substring(4,6)-1,
dateString.substring(6,8));
else if (dateType == 2)
var dob = new Date(dateString.substring(0,2),
dateString.substring(2,4)-1,
dateString.substring(4,6));
else if (dateType == 3)
var dob = new Date(dateString.substring(6,10),
dateString.substring(3,5)-1,
dateString.substring(0,2));
else if (dateType == 4)
var dob = new Date(dateString.substring(6,8),
dateString.substring(3,5)-1,
dateString.substring(0,2));
else
return ''; var yearDob = dob.getYear();
var monthDob = dob.getMonth();
var dateDob = dob.getDate(); yearAge = yearNow - yearDob; if (monthNow > monthDob)
var monthAge = monthNow - monthDob;
else {
yearAge--;
var monthAge = 12 + monthNow -monthDob;
}
if (dateNow > dateDob)
var dateAge = dateNow - dateDob;
else {
monthAge--;
var dateAge = 31 + dateNow - dateDob;
} return yearAge + ' years ' + monthAge + ' months ' + dateAge + ' days';
}
调用例子
document.write(getAge("19650104",1)+'BR>')
document.write(getAge("650104",2)+'BR>')
document.write(getAge("04/01/1965",3)+'BR>')
document.write(getAge("04/01/65",4)+'BR>')
//-SCRIPT>7。如何使用下面的格式dd/mm/yy在网页中显示日期
<SCRIPT LANGUAGE = 'JavaScript'>
<!-
var date = new Date();
var d = date.getDate();
var day = (d < 10) ? '0' + d : d;
var m = date.getMonth() + 1;
var month = (m < 10) ? '0' + m : m;
var yy = date.getYear();
var year = (yy < 1000) ? yy + 1900 : yy;document.write(day + "/" + month + "/" + year);
//-
</SCRIPT>8。如何使用下面的格式date month year在网页中显示日期
<SCRIPT LANGUAGE = 'JavaScript'>
<!-
function makeArray() {
for (i = 0; i<makeArray.arguments.length; i++)
this[i + 1] = makeArray.arguments[i];
}var months = new makeArray('January','February','March',
'April','May','June','July','August','September',
'October','November','December');var date = new Date();
var day = date.getDate();
var month = date.getMonth() + 1;
var yy = date.getYear();
var year = (yy < 1000) ? yy + 1900 : yy;document.write(day + " " + months[month] + " " + year);
//-
</SCRIPT>9.如何让我的网页的最近更新日期更易读
<SCRIPT LANGUAGE = 'JavaScript'><
function makeArray0() {
for (i = 0; i<makeArray0.arguments.length; i++)
this[i] = makeArray0.arguments[i];
}
var days = new makeArray0("Sunday","Monday","Tuesday","Wednesday",
"Thursday","Friday","Saturday");
var months = new makeArray0('January','February','March',
'April','May','June','July','August','September',
'October','November','December');
function nths(day) {
if (day == 1 || day == 21 || day == 31)
return 'st';
else
if (day == 2 || day == 22)
return 'nd';
if (day == 3 || day == 23)
return 'rd';
else return 'th';
}
function y2k(number) { return (number < 1000) ? number + 1900 : number; }
var last = document.lastModified;
var date = new Date(last);
document.write("Last updated on " + days[date.getDay()] + ' ' +
date.getDate() + nths(date.getDate()) + " " +
months[date.getMonth()] + ", " +
(y2k(date.getYear()) + "."
//-SCRIPT>10。如何显示到某个特定日期的倒记时
<SCRIPT LANGUAGE="JavaScript"><
function y2k(number) { return (number < 1000) ? number + 1900 : number; }function timeTillDate(whenDay,whenMonth,whenYear) {
var now = new Date();
var thisDay = now.getDate(), thisMonth = now.getMonth() + 1, thisYear = y2k(now.getYear())
var yearsDifference = whenYear - thisYear, monthsDifference = 0, daysDifference = 0, string = ''; if (whenMonth >= thisMonth) monthsDifference = whenMonth - thisMonth;
else { yearsDifference--; monthsDifference = whenMonth + 12 - thisMonth; } if (whenDay >= thisDay)daysDifference = whenDay - thisDay;
else {
if (monthsDifference > 0) monthsDifference--;
else { yearsDifference--; monthsDifference+=11; }
daysDifference = whenDay + 31 - thisDay;
} if (yearsDifference < 0) return ''; if ((yearsDifference == 0) && (monthsDifference == 0) && (daysDifference == 0))
return ''; if (yearsDifference > 0) {
string = yearsDifference + ' year';
if (yearsDifference > 1) string += 's';
string += ' ';
} if (monthsDifference > 0) {
string += monthsDifference + ' month';
if (monthsDifference > 1) string += 's';
string += ' ';
}
if (daysDifference > 0) {
string += daysDifference + ' day';
if (daysDifference > 1) string += 's';
string += ' ';
} var difference = Date.UTC(now.getYear(),now.getMonth(),now.getDate(),now.getHours(),now.getMinutes(),now.getSeconds()) -
Date.UTC(now.getYear(),now.getMonth(),now.getDate(),0,0,0); difference = 1000*60*60*24 - difference; var hoursDifference = Math.floor(difference/1000/60/60);
difference = difference - hoursDifference*1000*60*60
var minutesDifference = Math.floor(difference/1000/60);
difference = difference - minutesDifference*1000*60
var secondsDifference = Math.floor(difference/1000); if (hoursDifference > 0) {
string += hoursDifference + ' hour';
if (hoursDifference > 1) string +='s';
string += ' ';
} if (minutesDifference > 0) {
string += minutesDifference + ' minute';
if (minutesDifference > 1) string +='s';
string += ' ';
} if (secondsDifference > 0) {
string += secondsDifference + ' second';
if (secondsDifference > 1) string +='s';
string += ' ';
}
return string;
}调用例子,例如现在到31/12/1999还有多久。
document.write(timeTillDate(31,12,1999));
//-SCRIPT>11。如何从一个日期中减掉几个小时
<SCRIPT LANGUAGE = 'JavaScript'><
var date = new Date();
var date = new Date(Date.UTC(date.getYear(),date.getMonth(),date.getDate(),date.getHours(),date.getMinutes(),date.getSeconds()) - 5*60*60*1000);
document.write(date);
//-SCRIPT>12。如何在一个日期中增加几个月后并能够正确显示出来
<SCRIPT LANGUAGE="JavaScript"><
function makeArray() {
for (i = 0; i<makeArray.arguments.length; i++)
this[i + 1] = makeArray.arguments[i];
}
var months = new makeArray('January','February','March','April',
'May','June','July','August','September',
'October','November','December');
function nths(day) {
if (day == 1 || day == 21 || day == 31) return 'st';
else if (day == 2 || day == 22) return 'nd';
else if (day == 3 || day == 23) return 'rd';
else return 'th';
}
function y2k(number) { return (number < 1000) ? number + 1900 : number; }
function monthsahead(noofmonths) {
var today = new Date();
var date = new Date(today.getYear(),today.getMonth() + noofmonths,today.getDate(),today.getHours(),today.getMinutes(),today.getSeconds())
return date.getDate() + nths(date.getDate()) + ' ' + months[date.getMonth() + 1] + ' ' + y2k(date.getYear())
}调用例子:
document.write(monthsahead(6));
//-SCRIPT>
#calendar {
LEFT: 50px; VISIBILITY: hidden; POSITION: absolute; TOP: 50px; BACKGROUND-COLOR: white
}
TD.cal {
FONT-SIZE: 8pt; FONT-FAMILY: arial
}
TD.calmonth {
FONT-SIZE: 8pt; FONT-FAMILY: arial; TEXT-ALIGN: right
}
TD.caltoday {
BORDER-TOP-WIDTH: 1px; BORDER-LEFT-WIDTH: 1px; FONT-SIZE: 8pt; BORDER-LEFT-COLOR: #800000; BORDER-BOTTOM-WIDTH: 1px; BORDER-BOTTOM-COLOR: #800000; COLOR: white; BORDER-TOP-COLOR: #800000; FONT-FAMILY: arial; BACKGROUND-COLOR: #c0c0c0; TEXT-ALIGN: right; BORDER-RIGHT-WIDTH: 1px; BORDER-RIGHT-COLOR: #800000; border-type: solid
}
INPUT.caltoday {
FONT-SIZE: 8pt; WIDTH: 47px; FONT-FAMILY: arial; HEIGHT: 20px
}
A.cal {
COLOR: #000000; TEXT-DECORATION: none
}
A.calthismonth {
COLOR: #000000; TEXT-DECORATION: none
}
A.calothermonth {
COLOR: #808080; TEXT-DECORATION: none
}
</STYLE><SCRIPT language=JavaScript>
var use_css=false;
var use_layers=false;
if (document.all) { use_css = true; }
if (document.layers) { use_layers = true; }var CALWINDOW;
function writestyles(doc) {
var result = "";
result += "<STYLE>\n";
result += "TD.cal { font-family:arial; font-size: 8pt; }\n";
result += "TD.calmonth { font-family:arial; font-size: 8pt; text-align: right;}\n";
result += "TD.caltoday { font-family:arial; font-size: 8pt; text-align: right; color: white; background-color:#C0C0C0; border-width:1; border-type:solid; border-color:#800000; }\n";
result += "INPUT.caltoday { font-family:arial; font-size: 8pt; width:47px; height: 20px; }\n";
result += "A.cal { text-decoration:none; color:#000000; }\n";
result += "A.calthismonth { text-decoration:none; color:#000000; }\n";
result += "A.calothermonth { text-decoration:none; color:#808080; }\n";
result += "</STYLE>\n";
if (doc != "") {
doc.write(result);
}
else {
return result;
}
}
writestyles(this.document);function getOffsetLeft (el) {
var scrollamount = document.body.scrollLeft;
var ol = el.offsetLeft;
while ((el = el.offsetParent) != null) { ol += el.offsetLeft; }
ol = ol - scrollamount;
return ol;
}
function getOffsetTop (el) {
var scrollamount = document.body.scrollTop;
var ot = el.offsetTop;
while((el = el.offsetParent) != null) { ot += el.offsetTop; }
ot = ot - scrollamount;
return ot;
}
function showCalendar(divname, anchorname, functionname) {
if (use_css) {
var x = getOffsetLeft(document.all[anchorname]);
var y = getOffsetTop(document.all[anchorname]);
}
else if (use_layers) {
var found=0;
for (var i=0; i<document.anchors.length; i++) {
if (document.anchors[i].name == anchorname) {
found=1;
break;
}
}
if (found == 0) {
return;
}
var x = document.anchors[i].x;
var y = document.anchors[i].y;
x=x-window.pageXOffset;
y=y-window.pageYOffset;
}
else {
return;
}
x = x-152;
y = y+25;
if (divname != "") {
// Position the calendar DIV
if (use_layers) { var calendardiv = document.layers[divname]; }
if (use_css) { var calendardiv = document.all[divname].style; }
calendardiv.left = x;
calendardiv.top = y;
// Write output to calendar DIV
if (arguments.length>4) {
outputCalendar(divname,functionname,arguments[3],arguments[4]);
}
else {
outputCalendar(divname,functionname);
}
// Show the calendar DIV
calendardiv.visibility = "visible";
}
else {
if (use_layers) {
var windowx = window.screenX;
var windowy = window.screenY + (window.outerHeight-24-window.innerHeight);
}
if (use_css) {
var windowx = window.screenLeft;
var windowy = window.screenTop;
}
x = x + windowx;
y = y + windowy;
if (!CALWINDOW ¦¦ CALWINDOW.closed) {
CALWINDOW = window.open("about:blank","calwindow","status,width=150,height=175,screenX="+x+",left="+x+",screenY="+y+",top="+y+",resizable");
}
// Write output to popup window
if (arguments.length>4) {
outputCalendar(divname,functionname,arguments[3],arguments[4]);
}
else {
outputCalendar(divname,functionname);
}
}
}function hideCalendar(divname) {
if (divname != "") {
if (use_layers) { var calendardiv = document.layers[divname]; }
if (use_css) { var calendardiv = document.all[divname].style; }
calendardiv.visibility = "hidden";
}
else {
if (CALWINDOW && !CALWINDOW.closed) {
CALWINDOW.close();
}
}
}
function outputCalendar(divname, functionname) {
var now = new Date();
if (arguments.length > 2) { var month = arguments[2]; }
else { var month = now.getMonth()+1; }
if (arguments.length > 3) { var year = arguments[3]; }
else { var year = now.getFullYear(); }
var monthnames = new Array('一月','二月','三月','四月','五月','六月','七月','八月','九月','十月','十一月','十二月');
var daysinmonth= new Array(0,31,28,31,30,31,30,31,31,30,31,30,31); if ( ( (year%4 == 0)&&(year%100 != 0) ) ¦¦ (year%400 == 0) ) { // leap year
daysinmonth[2] = 29;
}
var current_month = new Date(year,month-1,1);
var display_year = year;
var display_month = month;
var display_date = 1; var offset = 0;
var weekday= current_month.getDay();
if (weekday > 0) {
display_month--;
if (display_month < 1) { display_month = 12; display_year--; }
display_date = daysinmonth[display_month]-weekday+1;
}
var next_month = month+1;
var next_month_year = year;
if (next_month > 12) { next_month=1; next_month_year++; }
var last_month = month-1;
var last_month_year = year;
if (last_month < 1) { last_month=12; last_month_year--; }
var date_class;
var result = "";
if (divname == "" ) {
var windowref = "window.opener.";
}
else {
var windowref = "";
}
if (divname == "") {
result += "<HTML><HEAD>"+writestyles('')+"<BODY MARGINWIDTH=0 MARGINHEIGHT=0 TOPMARGIN=0 RIGHTMARGIN=0 LEFTMARGIN=0>";
}
result += '<FORM>';
if (divname != "") {
result += '<TABLE WIDTH=144 BORDER=1 BORDERWIDTH=1 BORDERCOLOR="#808080" CELLSPACING=0 CELLPADDING=1>';
result += '<TR><TD ALIGN=CENTER>';
result += '<CENTER>';
result += '<TABLE WIDTH=144 BORDER=0 BORDERWIDTH=0 CELLSPACING=0 CELLPADDING=0>';
}
else {
result += '<CENTER><TABLE WIDTH=100% BORDER=0 BORDERWIDTH=0 CELLSPACING=0 CELLPADDING=0>';
}
result += '<TR BGCOLOR="#ff3366">';
result += ' <TD CLASS="cal" WIDTH=22 ALIGN=CENTER VALIGN=MIDDLE><B><A CLASS="cal" HREF="javascript:'+windowref+'outputCalendar(\''+divname+'\',\''+functionname+'\','+last_month+','+last_month_year+')"><<</A></B></TD>';
result += ' <TD CLASS="cal" WIDTH=100 ALIGN=CENTER>'+monthnames[month-1]+' '+year+'</TD>';
result += ' <TD CLASS="cal" WIDTH=22 ALIGN=CENTER VALIGN=MIDDLE><B><A CLASS="cal" HREF="javascript:'+windowref+'outputCalendar(\''+divname+'\',\''+functionname+'\','+next_month+','+next_month_year+')">>></A></B></TD>';
result += '</TR>';
result += '</TABLE>';
result += '<TABLE WIDTH=120 BORDER=0 CELLSPACING=1 CELLPADDING=0 ALIGN=CENTER>';
result += '<TR>';
result += ' <TD CLASS="cal" ALIGN=RIGHT WIDTH=14%>日</TD>';
result += ' <TD CLASS="cal" ALIGN=RIGHT WIDTH=14%>一</TD>';
result += ' <TD CLASS="cal" ALIGN=RIGHT WIDTH=14%>二</TD>';
result += ' <TD CLASS="cal" ALIGN=RIGHT WIDTH=14%>三</TD>';
result += ' <TD CLASS="cal" ALIGN=RIGHT WIDTH=14%>四</TD>';
result += ' <TD CLASS="cal" ALIGN=RIGHT WIDTH=14%>五</TD>';
result += ' <TD CLASS="cal" ALIGN=RIGHT WIDTH=14%>六</TD>';
result += '</TR>';
result += '<TR><TD COLSPAN=7 ALIGN=CENTER><IMG SRC="graypixel.gif" WIDTH=120 HEIGHT=1></TD></TR>';
for (var row=1; row<=6; row++) {
result += '<TR>';
for (var col=1; col<=7; col++) {
if (display_month == month) {
date_class = "calthismonth";
}
else {
date_class = "calothermonth";
}
if ((display_month == now.getMonth()+1) && (display_date==now.getDate()) && (display_year==now.getFullYear())) {
td_class="caltoday";
}
else {
td_class="calmonth";
}
result += ' <TD CLASS="'+td_class+'"><A HREF="javascript:'+windowref+functionname+'('+display_year+','+display_month+','+display_date+');'+windowref+'hideCalendar(\''+divname+'\');" CLASS="'+date_class+'">'+display_date+'</A></TD>';
display_date++;
if (display_date > daysinmonth[display_month]) {
display_date=1;
display_month++;
}
if (display_month > 12) {
display_month=1;
display_year++;
}
}
result += '</TR>';
}
result += '<TR><TD COLSPAN=7 ALIGN=CENTER><IMG SRC="graypixel.gif" WIDTH=120 HEIGHT=1></TD></TR>';
result += '<TR>';
result += ' <TD COLSPAN=7 ALIGN=CENTER>';
result += ' <INPUT CLASS="caltoday" TYPE="button" VALUE="Today" onClick="'+windowref+functionname+'(\''+now.getFullYear()+'\',\''+(now.getMonth()+1)+'\',\''+now.getDate()+'\');'+windowref+'hideCalendar(\''+divname+'\');">';
result += ' <BR>';
result += ' </TD>';
result += '</TR>';
result += '</TABLE>';
result += '</CENTER>';
result += '</TD></TR>';
result += '</TABLE>';
result += '</FORM>';
if (divname == "") {
result += "</BODY></HTML>";
} if (divname != "") {
if (use_css) {
document.all[divname].innerHTML = result;
}
if (use_layers) {
var thedoc = document.layers[divname].document;
thedoc.open;
thedoc.write(result);
thedoc.close();
}
}
else {
CALWINDOW.document.open();
CALWINDOW.document.write(result);
CALWINDOW.document.close();
}
}</SCRIPT><SCRIPT language=JavaScript>
function showdate( year, month, date ){var a = new Array('','一月','二月','三月','四月','五月','六月','七月','八月','九月','十月','十一月','十二月');
month = a[month];
document.forms[0].date1.value = ""+year+"年"+month+date+"日";
hideCalendar('calendar');
}
function clicklink() {
if (document.forms[0].caltype[0].checked) {
showCalendar('calendar','calposition1','showdate');}
else {showCalendar('','calposition1','showdate');
}
}
</SCRIPT>
<FORM>请选择日期:<INPUT type=radio CHECKED name=caltype>
<B>DIV</B> (DHTML方式) <INPUT type=radio name=caltype> <B>弹出窗口方式</B> <BR><BR>日期:
<INPUT name=date1><A name=calposition1> </A>[ <A
href="javascript:clicklink()">弹出选择框</A> ] <BR></FORM>
<DIV id=calendar></DIV>
下面是一个让用户选择日期的程序。
function foption(N){
var i,N,str,M;
str="";
for(i=1;i<=N;i++)
{if(i<10) str="<option value='0"+i+"'>"+"0"+i+"</option>";
else str="<option value='"+i+"'>"+i+"</option>";
document.write(str);
}
}function changeri(){
var nian,yue,ri;
ri=31;
yue=form1.selmonth .value ;
if(yue=="04" || yue=="06" || yue=="09" || yue=="11")
ri=30;
if(yue=="02")
{nian=form1.selyear.value;
if(nian%4==0 && nian%100!=0)
ri=29;
else ri=28;
}
flen=form1.selday .length ;
form1.selday.length =ri;
i=flen+1;
for(i;i<=ri;i++)
{
form1.selday .options(i-1).text=i;
form1.selday .options(i-1).value=i;
}
}
<select name="selyear" onchange="javascript:changeri();">
<script language="javascript">foption(12);</script>
</select>年<select name="selmonth" onchange="javascript:changeri();">
<script language="javascript">foption(12);</script>
</select>月<select name=selday>
<div id=selday><script language="javascript">foption(31);</script></div>
</select>日另外也可以做成日历的方式,我有一个用ASP写的日历,代码很简单,有意者请用EMAIL跟我联系。