Cheer 发表于 2007-4-28 10:59:35

时间日期日期选择填写脚本

把下列代码加到网页的<body></body>标签之间 从google下载我的照片管理软件
要完成此效果需要两个步骤

第一步:把如下代码加入到<head>区域中

<script>

var weekend = ;
var weekendcolor = "#e0e0e0";
var fontface = "verdana";
var fontsize = 2;

var gnow = new date();
var ggwincal;
isnav = (navigator.appname.indexof("netscape") != -1) ? true : false;
isie = (navigator.appname.indexof("microsoft") != -1) ? true : false;

calendar.months = ["一月", "二月", "三月", "四月", "五月", "六月",
"七月", "八月", "九月", "十月", "十一月", "十二月"];

// non-leap year month days..
calendar.domonth = ;
// leap year month days..
calendar.ldomonth = ;

function calendar(p_item, p_wincal, p_month, p_year, p_format) {
    if ((p_month == null) && (p_year == null))    return;

    if (p_wincal == null)
      this.gwincal = ggwincal;
    else
      this.gwincal = p_wincal;
   
    if (p_month == null) {
      this.gmonthname = null;
      this.gmonth = null;
      this.gyearly = true;
    } else {
      this.gmonthname = calendar.get_month(p_month);
      this.gmonth = new number(p_month);
      this.gyearly = false;
    }

    this.gyear = p_year;
    this.gformat = p_format;
    this.gbgcolor = "white";
    this.gfgcolor = "black";
    this.gtextcolor = "black";
    this.gheadercolor = "black";
    this.greturnitem = p_item;
}

calendar.get_month = calendar_get_month;
calendar.get_daysofmonth = calendar_get_daysofmonth;
calendar.calc_month_year = calendar_calc_month_year;
calendar.print = calendar_print;

function calendar_get_month(monthno) {
    return calendar.months;
}

function calendar_get_daysofmonth(monthno, p_year) {
    /*
    check for leap year ..
    1.years evenly divisible by four are normally leap years, except for...
    2.years also evenly divisible by 100 are not leap years, except for...
    3.years also evenly divisible by 400 are leap years.
    */
    if ((p_year % 4) == 0) {
      if ((p_year % 100) == 0 && (p_year % 400) != 0)
            return calendar.domonth;
   
      return calendar.ldomonth;
    } else
      return calendar.domonth;
}

function calendar_calc_month_year(p_month, p_year, incr) {
    /*
    will return an 1-d array with 1st element being the calculated month
    and second being the calculated year
    after applying the month increment/decrement as specified by 'incr' parameter.
    'incr' will normally have 1/-1 to navigate thru the months.
    */
    var ret_arr = new array();
   
    if (incr == -1) {
      // b a c k w a r d
      if (p_month == 0) {
            ret_arr = 11;
            ret_arr = parseint(p_year) - 1;
      }
      else {
            ret_arr = parseint(p_month) - 1;
            ret_arr = parseint(p_year);
      }
    } else if (incr == 1) {
      // f o r w a r d
      if (p_month == 11) {
            ret_arr = 0;
            ret_arr = parseint(p_year) + 1;
      }
      else {
            ret_arr = parseint(p_month) + 1;
            ret_arr = parseint(p_year);
      }
    }
   
    return ret_arr;
}

function calendar_print() {
    ggwincal.print();
}

function calendar_calc_month_year(p_month, p_year, incr) {
    /*
    will return an 1-d array with 1st element being the calculated month
    and second being the calculated year
    after applying the month increment/decrement as specified by 'incr' parameter.
    'incr' will normally have 1/-1 to navigate thru the months.
    */
    var ret_arr = new array();
   
    if (incr == -1) {
      // b a c k w a r d
      if (p_month == 0) {
            ret_arr = 11;
            ret_arr = parseint(p_year) - 1;
      }
      else {
            ret_arr = parseint(p_month) - 1;
            ret_arr = parseint(p_year);
      }
    } else if (incr == 1) {
      // f o r w a r d
      if (p_month == 11) {
            ret_arr = 0;
            ret_arr = parseint(p_year) + 1;
      }
      else {
            ret_arr = parseint(p_month) + 1;
            ret_arr = parseint(p_year);
      }
    }
   
    return ret_arr;
}

// this is for compatibility with navigator 3, we have to create and discard one object before the prototype object exists.
new calendar();

calendar.prototype.getmonthlycalendarcode = function() {
    var vcode = "";
    var vheader_code = "";
    var vdata_code = "";
   
    // begin table drawing code here..
    vcode = vcode + "<table border=1 bgcolor=\"" + this.gbgcolor + "\">";
   
    vheader_code = this.cal_header();
    vdata_code = this.cal_data();
    vcode = vcode + vheader_code + vdata_code;
   
    vcode = vcode + "</table>";
   
    return vcode;
}

calendar.prototype.show = function() {
    var vcode = "";
   
    this.gwincal.document.open();

    // setup the page...
    this.wwrite("<html>");
    this.wwrite("<head><title>typhoon start javascript fairyland</title>");
    this.wwrite("</head>");

    this.wwrite("<body " +
      "link=\"" + this.glinkcolor + "\" " +
      "vlink=\"" + this.glinkcolor + "\" " +
      "alink=\"" + this.glinkcolor + "\" " +
      "text=\"" + this.gtextcolor + "\">");
    this.wwritea("<font face='" + fontface + "' size=2><b>");
    this.wwritea(this.gmonthname + " " + this.gyear);
    this.wwritea("</b><br>");

    // show navigation buttons
    var prevmmyyyy = calendar.calc_month_year(this.gmonth, this.gyear, -1);
    var prevmm = prevmmyyyy;
    var prevyyyy = prevmmyyyy;

    var nextmmyyyy = calendar.calc_month_year(this.gmonth, this.gyear, 1);
    var nextmm = nextmmyyyy;
    var nextyyyy = nextmmyyyy;
   
    this.wwrite("<table width='100%' border=1 cellspacing=0 cellpadding=0 bgcolor='#e0e0e0'><tr><td align=center>");
    this.wwrite("[<a href=\"" +
      "javascript:window.opener.build(" +
      "'" + this.greturnitem + "', '" + this.gmonth + "', '" + (parseint(this.gyear)-1) + "', '" + this.gformat + "'" +
      ");" +
      "\"><<<\/a>]</td><td align=center>");
    this.wwrite("[<a href=\"" +
      "javascript:window.opener.build(" +
      "'" + this.greturnitem + "', '" + prevmm + "', '" + prevyyyy + "', '" + this.gformat + "'" +
      ");" +
      "\"><<\/a>]</td><td align=center>");
    this.wwrite("[<a href=\"javascript:window.print();\">打印</a>]</td><td align=center>");
    this.wwrite("[<a href=\"" +
      "javascript:window.opener.build(" +
      "'" + this.greturnitem + "', '" + nextmm + "', '" + nextyyyy + "', '" + this.gformat + "'" +
      ");" +
      "\">><\/a>]</td><td align=center>");
    this.wwrite("[<a href=\"" +
      "javascript:window.opener.build(" +
      "'" + this.greturnitem + "', '" + this.gmonth + "', '" + (parseint(this.gyear)+1) + "', '" + this.gformat + "'" +
      ");" +
      "\">>><\/a>]</td></tr></table><br>");

    // get the complete calendar code for the month..
    vcode = this.getmonthlycalendarcode();
    this.wwrite(vcode);

    this.wwrite("</font></body></html>");
    this.gwincal.document.close();
}

calendar.prototype.showy = function() {
    var vcode = "";
    var i;
    var vr, vc, vx, vy;      // row, column, x-coord, y-coord
    var vxf = 285;            // x-factor
    var vyf = 200;            // y-factor
    var vxm = 10;            // x-margin
    var vym;                // y-margin
    if (isie)    vym = 75;
    else if (isnav)    vym = 25;
   
    this.gwincal.document.open();

    this.wwrite("<html>");
    this.wwrite("<head><title>calendar</title>");
    this.wwrite("<style type='text/css'>\n<!--");
    for (i=0; i<12; i++) {
      vc = i % 3;
      if (i>=0 && i<= 2)    vr = 0;
      if (i>=3 && i<= 5)    vr = 1;
      if (i>=6 && i<= 8)    vr = 2;
      if (i>=9 && i<= 11)    vr = 3;
      
      vx = parseint(vxf * vc) + vxm;
      vy = parseint(vyf * vr) + vym;

      this.wwrite(".lclass" + i + " {position:absolute;top:" + vy + ";left:" + vx + ";}");
    }
    this.wwrite("-->\n</style>");
    this.wwrite("</head>");

    this.wwrite("<body " +
      "link=\"" + this.glinkcolor + "\" " +
      "vlink=\"" + this.glinkcolor + "\" " +
      "alink=\"" + this.glinkcolor + "\" " +
      "text=\"" + this.gtextcolor + "\">");
    this.wwrite("<font face='" + fontface + "' size=2><b>");
    this.wwrite("year : " + this.gyear);
    this.wwrite("</b><br>");

    // show navigation buttons
    var prevyyyy = parseint(this.gyear) - 1;
    var nextyyyy = parseint(this.gyear) + 1;
   
    this.wwrite("<table width='100%' border=1 cellspacing=0 cellpadding=0 bgcolor='#e0e0e0'><tr><td align=center>");
    this.wwrite("[<a href=\"" +
      "javascript:window.opener.build(" +
      "'" + this.greturnitem + "', null, '" + prevyyyy + "', '" + this.gformat + "'" +
      ");" +
      "\" alt='prev year'><<<\/a>]</td><td align=center>");
    this.wwrite("[<a href=\"javascript:window.print();\">打印</a>]</td><td align=center>");
    this.wwrite("[<a href=\"" +
      "javascript:window.opener.build(" +
      "'" + this.greturnitem + "', null, '" + nextyyyy + "', '" + this.gformat + "'" +
      ");" +
      "\">>><\/a>]</td></tr></table><br>");

    // get the complete calendar code for each month..
    var j;
    for (i=11; i>=0; i--) {
      if (isie)
            this.wwrite("<div id=\"layer" + i + "\" class=\"lclass" + i + "\">");
      else if (isnav)
            this.wwrite("<layer id=\"layer" + i + "\" class=\"lclass" + i + "\">");

      this.gmonth = i;
      this.gmonthname = calendar.get_month(this.gmonth);
      vcode = this.getmonthlycalendarcode();
      this.wwrite(this.gmonthname + "/" + this.gyear + "<br>");
      this.wwrite(vcode);

      if (isie)
            this.wwrite("</div>");
      else if (isnav)
            this.wwrite("</layer>");
    }

    this.wwrite("</font><br></body></html>");
    this.gwincal.document.close();
}

calendar.prototype.wwrite = function(wtext) {
    this.gwincal.document.writeln(wtext);
}

calendar.prototype.wwritea = function(wtext) {
    this.gwincal.document.write(wtext);
}

calendar.prototype.cal_header = function() {
    var vcode = "";
   
    vcode = vcode + "<tr>";
    vcode = vcode + "<td width='14%'><font size='2' face='" + fontface + "' color='" + this.gheadercolor + "'><b>日</b></font></td>";
    vcode = vcode + "<td width='14%'><font size='2' face='" + fontface + "' color='" + this.gheadercolor + "'><b>一</b></font></td>";
    vcode = vcode + "<td width='14%'><font size='2' face='" + fontface + "' color='" + this.gheadercolor + "'><b>二</b></font></td>";
    vcode = vcode + "<td width='14%'><font size='2' face='" + fontface + "' color='" + this.gheadercolor + "'><b>三</b></font></td>";
    vcode = vcode + "<td width='14%'><font size='2' face='" + fontface + "' color='" + this.gheadercolor + "'><b>四</b></font></td>";
    vcode = vcode + "<td width='14%'><font size='2' face='" + fontface + "' color='" + this.gheadercolor + "'><b>五</b></font></td>";
    vcode = vcode + "<td width='16%'><font size='2' face='" + fontface + "' color='" + this.gheadercolor + "'><b>六</b></font></td>";
    vcode = vcode + "</tr>";
   
    return vcode;
}

calendar.prototype.cal_data = function() {
    var vdate = new date();
    vdate.setdate(1);
    vdate.setmonth(this.gmonth);
    vdate.setfullyear(this.gyear);

    var vfirstday=vdate.getday();
    var vday=1;
    var vlastday=calendar.get_daysofmonth(this.gmonth, this.gyear);
    var vonlastday=0;
    var vcode = "";

    /*
    get day for the 1st of the requested month/year..
    place as many blank cells before the 1st day of the month as necessary.
    */

    vcode = vcode + "<tr>";
    for (i=0; i<vfirstday; i++) {
      vcode = vcode + "<td width='14%'" + this.write_weekend_string(i) + "><font size='2' face='" + fontface + "'> </font></td>";
    }

    // write rest of the 1st week
    for (j=vfirstday; j<7; j++) {
      vcode = vcode + "<td width='14%'" + this.write_weekend_string(j) + "><font size='2' face='" + fontface + "'>" +
            "<a href='#' " +
                "onclick=\"self.opener.document." + this.greturnitem + ".value='" +
                this.format_data(vday) +
                "';window.close();\">" +
                this.format_day(vday) +
            "</a>" +
            "</font></td>";
      vday=vday + 1;
    }
    vcode = vcode + "</tr>";

    // write the rest of the weeks
    for (k=2; k<7; k++) {
      vcode = vcode + "<tr>";

      for (j=0; j<7; j++) {
            vcode = vcode + "<td width='14%'" + this.write_weekend_string(j) + "><font size='2' face='" + fontface + "'>" +
                "<a href='#' " +
                  "onclick=\"self.opener.document." + this.greturnitem + ".value='" +
                  this.format_data(vday) +
                  "';window.close();\">" +
                this.format_day(vday) +
                "</a>" +
                "</font></td>";
            vday=vday + 1;

            if (vday > vlastday) {
                vonlastday = 1;
                break;
            }
      }

      if (j == 6)
            vcode = vcode + "</tr>";
      if (vonlastday == 1)
            break;
    }
   
    // fill up the rest of last week with proper blanks, so that we get proper square blocks
    for (m=1; m<(7-j); m++) {
      if (this.gyearly)
            vcode = vcode + "<td width='14%'" + this.write_weekend_string(j+m) +
            "><font size='2' face='" + fontface + "' color='gray'> </font></td>";
      else
            vcode = vcode + "<td width='14%'" + this.write_weekend_string(j+m) +
            "><font size='2' face='" + fontface + "' color='gray'>" + m + "</font></td>";
    }
   
    return vcode;
}

calendar.prototype.format_day = function(vday) {
    var vnowday = gnow.getdate();
    var vnowmonth = gnow.getmonth();
    var vnowyear = gnow.getfullyear();

    if (vday == vnowday && this.gmonth == vnowmonth && this.gyear == vnowyear)
      return ("<font color=\"red\"><b>" + vday + "</b></font>");
    else
      return (vday);
}

calendar.prototype.write_weekend_string = function(vday) {
    var i;

    // return special formatting for the weekend day.
    for (i=0; i<weekend.length; i++) {
      if (vday == weekend)
            return (" bgcolor=\"" + weekendcolor + "\"");
    }
   
    return "";
}

calendar.prototype.format_data = function(p_day) {
    var vdata;
    var vmonth = 1 + this.gmonth;
    vmonth = (vmonth.tostring().length < 2) ? "0" + vmonth : vmonth;
    var vmon = calendar.get_month(this.gmonth).substr(0,3).touppercase();
    var vfmon = calendar.get_month(this.gmonth).touppercase();
    var vy4 = new string(this.gyear);
    var vy2 = new string(this.gyear.substr(2,2));
    var vdd = (p_day.tostring().length < 2) ? "0" + p_day : p_day;

    switch (this.gformat) {
      case "mm\/dd\/yyyy" :
            vdata = vmonth + "\/" + vdd + "\/" + vy4;
            break;
      case "mm\/dd\/yy" :
            vdata = vmonth + "\/" + vdd + "\/" + vy2;
            break;
      case "mm-dd-yyyy" :
            vdata = vmonth + "-" + vdd + "-" + vy4;
            break;
      case "mm-dd-yy" :
            vdata = vmonth + "-" + vdd + "-" + vy2;
            break;

      case "dd\/mon\/yyyy" :
            vdata = vdd + "\/" + vmon + "\/" + vy4;
            break;
      case "dd\/mon\/yy" :
            vdata = vdd + "\/" + vmon + "\/" + vy2;
            break;
      case "dd-mon-yyyy" :
            vdata = vdd + "-" + vmon + "-" + vy4;
            break;
      case "dd-mon-yy" :
            vdata = vdd + "-" + vmon + "-" + vy2;
            break;

      case "dd\/month\/yyyy" :
            vdata = vdd + "\/" + vfmon + "\/" + vy4;
            break;
      case "dd\/month\/yy" :
            vdata = vdd + "\/" + vfmon + "\/" + vy2;
            break;
      case "dd-month-yyyy" :
            vdata = vdd + "-" + vfmon + "-" + vy4;
            break;
      case "dd-month-yy" :
            vdata = vdd + "-" + vfmon + "-" + vy2;
            break;

      case "dd\/mm\/yyyy" :
            vdata = vdd + "\/" + vmonth + "\/" + vy4;
            break;
      case "dd\/mm\/yy" :
            vdata = vdd + "\/" + vmonth + "\/" + vy2;
            break;
      case "dd-mm-yyyy" :
            vdata = vdd + "-" + vmonth + "-" + vy4;
            break;
      case "dd-mm-yy" :
            vdata = vdd + "-" + vmonth + "-" + vy2;
            break;

      default :
            vdata = vmonth + "\/" + vdd + "\/" + vy4;
    }

    return vdata;
}

function build(p_item, p_month, p_year, p_format) {
    var p_wincal = ggwincal;
    gcal = new calendar(p_item, p_wincal, p_month, p_year, p_format);

    // customize your calendar here..
    gcal.gbgcolor="white";
    gcal.glinkcolor="black";
    gcal.gtextcolor="black";
    gcal.gheadercolor="darkgreen";

    // choose appropriate show function
    if (gcal.gyearly)    gcal.showy();
    else    gcal.show();
}

function show_calendar() {
    /*
      p_month : 0-11 for jan-dec; 12 for all months.
      p_year    : 4-digit year
      p_format: date format (mm/dd/yyyy, dd/mm/yy, ...)
      p_item    : return item.
    */

    p_item = arguments;
    if (arguments == null)
      p_month = new string(gnow.getmonth());
    else
      p_month = arguments;
    if (arguments == "" || arguments == null)
      p_year = new string(gnow.getfullyear().tostring());
    else
      p_year = arguments;
    if (arguments == null)
      p_format = "mm/dd/yyyy";
    else
      p_format = arguments;

    vwincal = window.open("", "calendar",
      "width=250,height=250,status=no,resizable=no,top=200,left=200");
    vwincal.opener = self;
    ggwincal = vwincal;

    build(p_item, p_month, p_year, p_format);
}
/*
yearly calendar code starts here
*/
function show_yearly_calendar(p_item, p_year, p_format) {
    // load the defaults..
    if (p_year == null || p_year == "")
      p_year = new string(gnow.getfullyear().tostring());
    if (p_format == null || p_format == "")
      p_format = "mm/dd/yyyy";

    var vwincal = window.open("", "calendar", "scrollbars=yes");
    vwincal.opener = self;
    ggwincal = vwincal;

    build(p_item, null, p_year, p_format);
}


</script>


第二步:把如下代码加入到<body>区域中

<center>
<form name=calform>
<input type=text name="datebox" size=15>
    <a href="javascript:show_calendar('calform.datebox');" onmouseover="window.status='date picker';return true;" onmouseout="window.status='';return true;"><img src="http://www.jsshow.com/data46/show.gif" width=24 height=22 border=0>http://www.jsshow.com/data46/show.gif</a>
</form>
</center>
页: [1]
查看完整版本: 时间日期日期选择填写脚本