javascript做的日历,完全对象化,望高手提出改进意见。(2/3,将3部分拼成一个html文件浏览)
发表于:2007-06-30来源:作者:点击数:
标签:
this.GetTextDate = function(theString){ //convert a string to a date, if the string is not a date, return a empty string var i = 0, tmpChar = , find_tag = ; var start_at = 0, end_at = 0, year_at = 0, month_at = 0, day_at = 0; var tmp_at = 0
this.GetTextDate = function(theString){ //convert a string to a date, if the string is not a date, return a empty string
var i = 0, tmpChar = "", find_tag = "";
var start_at = 0, end_at = 0, year_at = 0, month_at = 0, day_at = 0;
var tmp_at = 0, one_at = 0, two_at = 0, one_days = 0, two_days = 0;
var aryDate = new Array();
var tmpYear = -1, tmpMonth = -1, tmpDay = -1;
var tmpDate = theString.toLowerCase();
var defDate = "";
end_at = tmpDate.length;
for (i=1;i<end_at;i++){
if (tmpDate.charAt(i)=="0"){
tmpChar = tmpDate.charAt(i-1);
if (tmpChar<"0" || tmpChar>"9"){
tmpDate = tmpDate.substr(0,i-1) + "-" + tmpDate.substr(i+1);
}
}
}
for (i=0;i<9;i++){
tmpDate = tmpDate.replace(this.MonthName[i].toLowerCase().substr(0,3), "-00" + (i+1).toString() + "-");
}
for (i=9;i<12;i++){
tmpDate = tmpDate.replace(this.MonthName[i].toLowerCase().substr(0,3), "-0" + (i+1).toString() + "-");
}
tmpDate = tmpDate.replace(/jan/g, "-001-");
tmpDate = tmpDate.replace(/feb/g, "-002-");
tmpDate = tmpDate.replace(/mar/g, "-003-");
tmpDate = tmpDate.replace(/apr/g, "-004-");
tmpDate = tmpDate.replace(/may/g, "-005-");
tmpDate = tmpDate.replace(/jun/g, "-006-");
tmpDate = tmpDate.replace(/jul/g, "-007-");
tmpDate = tmpDate.replace(/aug/g, "-008-");
tmpDate = tmpDate.replace(/sep/g, "-009-");
tmpDate = tmpDate.replace(/oct/g, "-010-");
tmpDate = tmpDate.replace(/nov/g, "-011-");
tmpDate = tmpDate.replace(/dec/g, "-012-");
for (i=0;i<tmpDate.length;i++){
tmpChar = tmpDate.charAt(i);
if ((tmpChar<"0" || tmpChar>"9") && (tmpChar != "-")){
tmpDate = tmpDate.replace(tmpChar,"-")
}
}
while(tmpDate.indexOf("--") != -1){
tmpDate = tmpDate.replace(/--/g,"-");
}
start_at = 0;
end_at = tmpDate.length-1;
while (tmpDate.charAt(start_at)=="-"){
start_at++;
}
while (tmpDate.charAt(end_at)=="-"){
end_at--;
}
if (start_at < end_at+1){
tmpDate = tmpDate.substring(start_at,end_at+1);
}else{
tmpDate = "";
}
aryDate = tmpDate.split("-");
if (aryDate.length != 3){
return(defDate);
}
for (i=0;i<3;i++){
if (parseInt(aryDate[i],10)<1){
aryDate[i] = "1";
}
}
find_tag="000";
for (i=2;i>=0;i--){
if (aryDate[i].length==3){
if (aryDate[i]>="001" && aryDate[i]<="012"){
tmpMonth = parseInt(aryDate[i],10)-1;
switch (i){
case 0:
find_tag = "100";
one_at = parseInt(aryDate[1],10);
two_at = parseInt(aryDate[2],10);
break;
case 1:
find_tag = "010";
one_at = parseInt(aryDate[0],10);
two_at = parseInt(aryDate[2],10);
break;
case 2:
find_tag = "001";
one_at = parseInt(aryDate[0],10);
two_at = parseInt(aryDate[1],10);
break;
}
}
}
}
if (find_tag!="000"){
one_days = this.GetMonthDays(two_at,tmpMonth);
two_days = this.GetMonthDays(one_at,tmpMonth);
if ((one_at>one_days)&&(two_at>two_days)){
return(defDate);
}
if ((one_at<=one_days)&&(two_at>two_days)){
tmpYear = this.GetFormatYear(two_at);
tmpDay = one_at;
}
if ((one_at>one_days)&&(two_at<=two_days)){
tmpYear = this.GetFormatYear(one_at);
tmpDay = two_at;
}
if ((one_at<=one_days)&&(two_at<=two_days)){
tmpYear = this.GetFormatYear(one_at);
tmpDay = two_at;
tmpDate = this.DateFormat;
year_at = tmpDate.indexOf("<yyyy>");
if (year_at == -1){
year_at = tmpDate.indexOf("<yy>");
}
day_at = tmpDate.indexOf("<dd>");
if (day_at == -1){
day_at = tmpDate.indexOf("<d>");
}
if (year_at >= day_at){
tmpYear = this.GetFormatYear(two_at);
tmpDay = one_at;
}
}
return(new Date(tmpYear, tmpMonth, tmpDay));
}
find_tag = "000";
for (i=2;i>=0;i--){
if (parseInt(aryDate[i],10)>31){
tmpYear = this.GetFormatYear(parseInt(aryDate[i],10));
switch (i){
case 0:
find_tag = "100";
one_at = parseInt(aryDate[1],10);
two_at = parseInt(aryDate[2],10);
break;
case 1:
find_tag = "010";
one_at = parseInt(aryDate[0],10);
two_at = parseInt(aryDate[2],10);
break;
case 2:
find_tag = "001";
one_at = parseInt(aryDate[0],10);
two_at = parseInt(aryDate[1],10);
break;
}
}
}
if (find_tag=="000"){
tmpDate = this.DateFormat;
year_at = tmpDate.indexOf("<yyyy>");
if (year_at == -1){
year_at = tmpDate.indexOf("<yy>");
}
month_at = tmpDate.indexOf("<MMMMMM>");
if (month_at == -1){
month_at = tmpDate.indexOf("<MMM>");
}
if (month_at == -1){
month_at = tmpDate.indexOf("<mm>");
}
if (month_at == -1){
month_at = tmpDate.indexOf("<m>");
}
day_at = tmpDate.indexOf("<dd>");
if (day_at == -1){
day_at = tmpDate.indexOf("<d>");
}
if ((year_at>month_at)&&(year_at>day_at)){
find_tag="001"
}
if ((year_at>month_at)&&(year_at<=day_at)){
find_tag="010";
}
if ((year_at<=month_at)&&(year_at>day_at)){
find_tag="010";
}
if ((year_at<=month_at)&&(year_at<=day_at)){
find_tag="100";
}
switch (find_tag){
case "100":
tmpYear = parseInt(aryDate[0],10);
one_at = parseInt(aryDate[1],10);
two_at = parseInt(aryDate[2],10);
break;
case "010":
one_at = parseInt(aryDate[0],10);
tmpYear = parseInt(aryDate[1],10);
two_at = parseInt(aryDate[2],10);
break;
case "001":
one_at = parseInt(aryDate[0],10);
two_at = parseInt(aryDate[1],10);
tmpYear = parseInt(aryDate[2],10);
break;
}
tmpYear = this.GetFormatYear(tmpYear);
}
if (find_tag!="000"){
if ((one_at>12)&&(two_at>12)){
return(defDate);
}
if (one_at<=12){
if (two_at > this.GetMonthDays(tmpYear,one_at-1)){
return(new Date(tmpYear, one_at-1, this.GetMonthDays(tmpYear,one_at-1)));
}
if (two_at>12){
return(new Date(tmpYear, one_at-1, two_at));
}
}
if (two_at<=12){
if (one_at > this.GetMonthDays(tmpYear,two_at-1)){
return(new Date(tmpYear, two_at-1, this.GetMonthDays(tmpYear,two_at-1)));
}
if (one_at>12){
return(new Date(tmpYear, two_at-1, one_at));
}
}
if ((one_at<=12)&&(two_at<=12)){
tmpMonth = one_at-1;
tmpDay = two_at;
tmpDate = this.DateFormat;
month_at = tmpDate.indexOf("<MMMMMM>");
if (month_at == -1){
month_at = tmpDate.indexOf("<MMM>");
}
if (month_at == -1){
month_at = tmpDate.indexOf("<mm>");
}
if (month_at == -1){
month_at = tmpDate.indexOf("<m>");
}
day_at = tmpDate.indexOf("<dd>");
if (day_at == -1){
day_at = tmpDate.indexOf("<d>");
}
if (month_at >= day_at){
tmpMonth = two_at-1;
tmpDay = one_at;
}
return(new Date(tmpYear, tmpMonth, tmpDay));
}
}
}
this.CreateYearList = function(MinYear, MaxYear){ //create year list
var theName = this.Name;
var theYearObject = document.all.item(theName + "_YearList");
if (theYearObject == null){
return;
}
var theYear = 0;
var theYearHTML = "<select id=\"" + theName + "_YearList\" style=\"" + this.YearListStyle + "\" tabIndex=\"-1\" onChange=\"document.jsMonthView.UpdateMonthGrid(this)\" onBlur=\"document.jsMonthView.DeleteMonthGrid()\">";
for (theYear = MinYear; theYear <= MaxYear; theYear++){
theYearHTML += "<option value=\"" + theYear.toString() + "\">" + theYear.toString() + "</option>";
}
theYearHTML += "</select>";
theYearObject.outerHTML = theYearHTML;
}
this.CreateMonthList = function( ){ //create month list
var theName = this.Name;
var theMonthObject = document.all.item(theName + "_MonthList");
if (theMonthObject == null){
return;
}
var theMonth = 0;
var theMonthHTML = "<select id=\"" + theName + "_MonthList\" style=\"" + this.MonthListStyle + "\" tabIndex=\"-1\" onChange=\"document.jsMonthView.UpdateMonthGrid(this)\" onBlur=\"document.jsMonthView.DeleteMonthGrid()\">";
for (theMonth = 0; theMonth < 12; theMonth++){
theMonthHTML += "<option value=\"" + theMonth.toString() + "\">" + this.MonthName[theMonth] + "</option>";
}
theMonthHTML +="</select>";
theMonthObject.outerHTML = theMonthHTML;
}
this.setDayList = function(theYear, theMonth, theDay){ //set the month view show a date
var theName = this.Name;
var theDayObject = document.all.item(theName + "_DayList");
if (theDayObject == null){
return;
}
theDayObject.value = theDay.toString();
var theFirstDay = new Date(theYear, theMonth, 1);
var theCurrentDate = new Date();
var theWeek = theFirstDay.getDay();
if (theWeek == 0){
theWeek = 7;
}
var theLeftDay = 0;
if (theMonth == 0){
theLeftDay = 31;
}else{
theLeftDay = this.GetMonthDays(theYear, theMonth - 1);
}
var theRightDay = this.GetMonthDays(theYear, theMonth);
var theCurrentDay = theLeftDay - theWeek + 1;
var offsetMonth = -1; //the month is previous month
var theColor = this.InvalidColor;
var theBgColor = this.UnselectBgColor;
var theBdColor = theBgColor;
var WeekId = 0
var DayId = 0;
var theStyle = "";
var theDayHTML = "<table width=\"100%\" height=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";
theDayHTML += " <tr style=\"" + this.TitleStyle + "\">";
for (DayId = 0; DayId < 7; DayId++){
theDayHTML += " <
td width=\"10%\">" + this.WeekName[DayId] + "</td>";
}
theDayHTML += " </tr>";
theDayHTML += " <tr>";
theDayHTML += " <td colspan=\"7\" style=\"" + this.LineBgStyle + "\">";
theDayHTML += " <table style=\"" + this.LineStyle + "\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";
theDayHTML += " <tr><td></td></tr>";
theDayHTML += " </table>";
theDayHTML += " </td>";
theDayHTML += " </tr>";
for (WeekId = 0; WeekId < 6; WeekId++){
theDayHTML += " <tr style=\"" + this.DayStyle + "\">";
for (DayId = 0; DayId < 7; DayId++){
if ((theCurrentDay > theLeftDay) && (WeekId < 3)){
offsetMonth++; //the month is current month;
theCurrentDay = 1;
}
if ((theCurrentDay > theRightDay) && (WeekId > 3)){
offsetMonth++; //the month is next month;
theCurrentDay = 1;
}
switch (offsetMonth){
case -1:
theColor = this.InvalidColor;
break;
case 1:
theColor = this.InvalidColor;
break;
case 0:
if ((DayId==0)||(DayId==6)){
theColor = this.WeekendColor;
}else{
theColor = this.ValidColor;
}
break;
}
if ((DayId==0)||(DayId==6)){
theBgColor = this.WeekendBgColor;
}else{
theBgColor = this.UnselectBgColor;
}
theBdColor = this.DayBdColor;
if ((theCurrentDay == theDay) && (offsetMonth == 0)){
theColor = this.SelectedColor;
theBgColor = this.SelectedBgColor;
theBdColor = theBgColor;
}
if ((theYear == theCurrentDate.getFullYear()) && (theMonth == theCurrentDate.getMonth()) && (theCurrentDay == theCurrentDate.getDate()) && (offsetMonth == 0)){
theBdColor = this.TodayBdColor;
}
theStyle = "border:" + this.DayBdWidth + "px solid " + theBdColor + "; color:" + theColor + "; background-color:" + theBgColor + ";";
theDayHTML += " <td style=\"" + theStyle + "\" onMouseOver=\"" + this.OverDayStyle + "\" onMouseOut=\"" + this.OutDayStyle + "\" onMouseDown=\"document.jsMonthView.CreateMonthGrid(" + theYear.toString() + ", " + (theMonth + offsetMonth).toString() + ", " + theCurrentDay.toString() + ")\">";
theDayHTML += theCurrentDay.toString();
theDayHTML += " </td>";
theCurrentDay++;
}
theDayHTML += " </tr>";
}
theDayHTML += " <tr style=\"" + this.FooterStyle + "\" onMouseDown=\"document.jsMonthView.CreateMonthGrid(" + theCurrentDate.getFullYear().toString() + ", " + theCurrentDate.getMonth().toString() + ", " + theCurrentDate.getDate().toString() + ");\">";
theStyle = "border:" + this.DayBdWidth + "px solid " + this.TodayBdColor + ";";
theDayHTML += " <td style=\"" + theStyle + "\"><br></td>";
theDayHTML += " <td colspan=\"6\"> " + this.TodayTitle + " " + this.SetDateFormat(theCurrentDate.getFullYear(), theCurrentDate.getMonth(), theCurrentDate.getDate()) + "</td>";
theDayHTML += " </tr>";
theDayHTML += " </table>";
var theMonthGrid = document.all.item(theName + "_MonthGrid");
theMonthGrid.innerHTML = theDayHTML;
}
原文转自:http://www.ltesting.net