Date.prototype.dateDiff = function(interval,objDate2) 
{ 
    var d=this, i={}, t=d.getTime(), t2=objDate2.getTime(); 
    i['y']=objDate2.getFullYear()-d.getFullYear(); 
    i['q']=i['y']*4+Math.floor(objDate2.getMonth()/4)-Math.floor(d.getMonth()/4); 
    i['m']=i['y']*12+objDate2.getMonth()-d.getMonth(); 
    i['ms']=objDate2.getTime()-d.getTime(); 
    i['w']=Math.floor((t2+345600000)/(604800000))-Math.floor((t+345600000)/(604800000)); 
    i['d']=Math.floor(t2/86400000)-Math.floor(t/86400000); 
    i['h']=Math.floor(t2/3600000)-Math.floor(t/3600000); 
    i['n']=Math.floor(t2/60000)-Math.floor(t/60000); 
    i['s']=Math.floor(t2/1000)-Math.floor(t/1000); 
    return i[interval]; 
} 
Date.prototype.DateAdd = function(strInterval, Number){    
    var dtTmp = this;   
    switch (strInterval){    
        case 's' :return new Date(Date.parse(dtTmp) + (1000 * Number));   
        case 'n' :return new Date(Date.parse(dtTmp) + (60000 * Number));   
        case 'h' :return new Date(Date.parse(dtTmp) + (3600000 * Number));   
        case 'd' :return new Date(Date.parse(dtTmp) + (86400000 * Number));   
        case 'w' :return new Date(Date.parse(dtTmp) + ((86400000 * 7) * Number));   
        case 'q' :return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()) + Number*3, dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds());   
        case 'm' :return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()) + Number, dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds());   
        case 'y' :return new Date((dtTmp.getFullYear() + Number), dtTmp.getMonth(), dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds());   
    }   
}

var DateLayer = '<div id=meizzDateLayer style="position: absolute; width:142px; height:166px;z-index:9998; display: none">';
DateLayer += '<iframe style="z-index:1;position:absolute;top:0;left:0;width:100%; height:100%;"></iframe>';
DateLayer+='<span id=tmpSelectYearLayer  style="z-index: 9999;position:absolute;top:2px;left:18px;display: none"></span>';
DateLayer+='<span id=tmpSelectMonthLayer style="z-index: 9999;position:absolute;top:2px;left:75px;display: none"></span>';
DateLayer += '<table border=0 cellspacing=1 cellpadding=0 width=142 height=160 bgcolor=#808080 onselectstart="return false" style="z-index:11;position:absolute;top:0;left:0;">';
DateLayer+='  <tr><td width=142 height=23 bgcolor=#FFFFFF><table border=0 cellspacing=1 cellpadding=0 width=140 height=23>';
DateLayer+='      <tr align=center><td width=20 align=center bgcolor=#808080 style="font-size:12px;cursor: pointer;color: #FFD700" ';
DateLayer+='        onclick="meizzPrevM()" title="向前翻　月" Author=meizz><b Author=meizz>&lt;&lt;</b>';
DateLayer+='        </td><td width=100 align=center style="font-size:12px;cursor:default" Author=meizz>';
DateLayer+='        <span Author=meizz id=meizzYearHead onclick="tmpSelectYearInnerHTML(testInnerText(this))"></span>&nbsp;年&nbsp;<span';
DateLayer+='         id=meizzMonthHead Author=meizz onclick="tmpSelectMonthInnerHTML(testInnerText(this))"></span>&nbsp;月</td>';
DateLayer+='        <td width=20 bgcolor=#808080 align=center style="font-size:12px;cursor: pointer;color: #FFD700" ';
DateLayer+='         onclick="meizzNextM()" title="往后翻　月" Author=meizz><b Author=meizz>&gt;&gt;</b></td></tr>';
DateLayer+='    </table></td></tr>';
DateLayer+='  <tr><td width=142 height=18 bgcolor=#808080>';
DateLayer+='<table border=0 cellspacing=0 cellpadding=0 width=140 height=1 style="cursor:default">';
DateLayer+='<tr align=center><td style="font-size:12px;color:#FFFFFF" Author=meizz>日</td>';
DateLayer+='<td style="font-size:12px;color:#FFFFFF" Author=meizz>一</td><td style="font-size:12px;color:#FFFFFF" Author=meizz>二</td>';
DateLayer+='<td style="font-size:12px;color:#FFFFFF" Author=meizz>三</td><td style="font-size:12px;color:#FFFFFF" Author=meizz>四</td>';
DateLayer+='<td style="font-size:12px;color:#FFFFFF" Author=meizz>五</td><td style="font-size:12px;color:#FFFFFF" Author=meizz>六</td></tr>';
DateLayer+='</table></td></tr><!-- Author:F.R.Huang(meizz) http://www.meizz.com/ mail: meizz@hzcnc.com 2002-10-8 -->';
DateLayer+='  <tr><td width=142 height=120>';
DateLayer+='    <table border=0 cellspacing=1 cellpadding=0 width=140 height=120 bgcolor=#FFFFFF>';
var n=0;
for (j=0;j<5;j++){
	DateLayer+=' <tr align=center>';
	for (i=0;i<7;i++){
		DateLayer+='<td width=20 height=20 id=meizzDay'+n+' style="font-size:12px" Author=meizz onclick=meizzDayClick(testInnerText(this))></td>';
		n++;
	}
	DateLayer+='</tr>';
}
DateLayer+='      <tr align=center><td width=20 height=20 style="font-size:12px" id=meizzDay35 Author=meizz ';
DateLayer+='         onclick=meizzDayClick(testInnerText(this))></td>';
DateLayer+='        <td width=20 height=20 style="font-size:12px" id=meizzDay36 Author=meizz onclick=meizzDayClick(testInnerText(this))></td></tr>';
DateLayer+='    </table></td></tr><tr><td>';
DateLayer+='        <table border=0 cellspacing=1 cellpadding=0 width=100% bgcolor=#FFFFFF>';
DateLayer+='          <tr><td Author=meizz align=left><input Author=meizz type=button value="<<" title="向前翻　年" onclick="meizzPrevY()" ';
DateLayer+='             onfocus="this.blur()" style="font-size: 12px; height: 20px"><input Author=meizz title="向前翻　月" type=button ';
DateLayer+='             value="<" onclick="meizzPrevM()" onfocus="this.blur()" style="font-size: 12px; height: 20px"></td><td ';
DateLayer+='             Author=meizz align=center><input Author=meizz type=button value=今天 onclick="meizzToday()" ';
DateLayer+='             onfocus="this.blur()" title="转到今天的日期" style="font-size: 12px; height: 20px"></td><td ';
DateLayer+='             Author=meizz align=right><input Author=meizz type=button value=">" onclick="meizzNextM()" ';
DateLayer+='             onfocus="this.blur()" title="往后翻　月" style="font-size: 12px; height: 20px"><input ';
DateLayer+='             Author=meizz type=button value=">>" title="往后翻　年" onclick="meizzNextY()"';
DateLayer+='             onfocus="this.blur()" style="font-size: 12px; height: 20px"></td>';
DateLayer+='</tr></table></td></tr></table></div>';　

//==================================================== WEB 页面显示部分 ======================================================
var outObject,CallbackFunction;

var MonHead = new Array(12);               //定义阳历中每个月的最大天数
    MonHead[0] = 31; MonHead[1] = 28; MonHead[2] = 31; MonHead[3] = 30; MonHead[4]  = 31; MonHead[5]  = 30;
    MonHead[6] = 31; MonHead[7] = 31; MonHead[8] = 30; MonHead[9] = 31; MonHead[10] = 30; MonHead[11] = 31;　

var meizzTheYear=new Date().getFullYear(); //定义年的变量的初始值
var meizzTheMonth=new Date().getMonth()+1; //定义月的变量的初始值
var meizzWDay=new Array(37);               //定义写日期的数组　


function setday(tt,obj,f) //主调函数
{
  if (arguments.length == 0){alert("对不起！您没有传回本控件任何参数！");return;}
  if(!document.getElementById('meizzDateLayer'))
  {
	  $(DateLayer).appendTo("body"); 
	  meizzSetDay(meizzTheYear,meizzTheMonth);
  }
  var dads  = document.getElementById('meizzDateLayer').style;var th = tt;
  var ttop  = tt.offsetTop;     //TT控件的定位点高
  var thei  = tt.clientHeight;  //TT控件本身的高
  var tleft = tt.offsetLeft;    //TT控件的定位点宽
  var ttyp  = tt.type;          //TT控件的类型
  while (tt = tt.offsetParent){ttop+=tt.offsetTop; tleft+=tt.offsetLeft;}
  dads.top  = ((ttyp=="image")? ttop+thei : ttop+thei+6)+'px';
  dads.left = tleft+'px';
  outObject = (typeof(obj)=="object")?obj:th;
  CallbackFunction = (typeof(f)=="function")?f:'';
  if(ttyp=='text' && th.value!='undefined' && th.value!='')
  {
		var d = new Date(Date.parse(th.value.replace(/-/g,"/")));
		meizzSetDay(d.getFullYear(),d.getMonth()+1);
  }
  dads.display = '';
  //event.returnValue=false;
  return false;
}
document.onclick=function(e)  //任意点击时关闭该控件
{ 
    e   =   getEvent();
    if(!e || !document.getElementById('meizzDateLayer'))return;
    var obj = e.srcElement   ||   e.target;
  with(obj)
  {
	  if (tagName != "INPUT" && getAttribute("Author")==null)
    document.getElementById('meizzDateLayer').style.display="none";
  }
}　

function meizzWriteHead(yy,mm)  //往 head 中写入当前的年与月
  { document.getElementById('meizzYearHead').innerHTML  = yy;
    document.getElementById('meizzMonthHead').innerHTML = mm;
  }　

function tmpSelectYearInnerHTML(strYear) //年份的下拉框
{
  if (strYear.match(/\D/)!=null){alert("年份输入参数不是数字！");return;}
  var m = (strYear) ? strYear : new Date().getFullYear();
  if (m < 1000 || m > 9999) {alert("年份值不在 1000 到 9999 之间！");return;}
  var n = m - 10;
  if (n < 1000) n = 1000;
  if (n + 26 > 9999) n = 9974;
  var s = '<select Author="meizz" name="tmpSelectYear" style="font-size:12px" onblur="document.getElementById(\'tmpSelectYearLayer\').style.display=\'none\'" onchange="document.getElementById(\'tmpSelectYearLayer\').style.display=\'none\';meizzTheYear=this.value;meizzSetDay(meizzTheYear,meizzTheMonth);">';
  var selectInnerHTML = s;
  for (var i = n; i < n + 26; i++)
  {
    if (i == m)
       {selectInnerHTML += "<option value='" + i + "' selected>" + i + "年" + "</option>\r\n";}
    else {selectInnerHTML += "<option value='" + i + "'>" + i + "年" + "</option>\r\n";}
  }
  selectInnerHTML += "</select>";
  document.getElementById('tmpSelectYearLayer').style.display="";
  document.getElementById('tmpSelectYearLayer').innerHTML = selectInnerHTML;
  document.getElementById('tmpSelectYear').focus();
}　

function tmpSelectMonthInnerHTML(strMonth) //月份的下拉框
{
  if (strMonth.match(/\D/)!=null){alert("月份输入参数不是数字！");return;}
  var m = (strMonth) ? strMonth : new Date().getMonth() + 1;
  var s = '<select Author="meizz" name="tmpSelectMonth" style="font-size: 12px" onblur="document.getElementById(\'tmpSelectMonthLayer\').style.display=\'none\'" onchange="document.getElementById(\'tmpSelectMonthLayer\').style.display=\'none\';meizzTheMonth=this.value;meizzSetDay(meizzTheYear,meizzTheMonth);">';
  var selectInnerHTML = s;
  for (var i = 1; i < 13; i++)
  {
    if (i == m)
       {selectInnerHTML += "<option value='"+i+"' selected>"+i+"月"+"</option>\r\n";}
    else {selectInnerHTML += "<option value='"+i+"'>"+i+"月"+"</option>\r\n";}
  }
  selectInnerHTML += "</select>";
  document.getElementById('tmpSelectMonthLayer').style.display="";
  document.getElementById('tmpSelectMonthLayer').innerHTML = selectInnerHTML;
  document.getElementById('tmpSelectMonth').focus();
}　

function closeLayer()               //这个层的关闭
  {
    document.getElementById('meizzDateLayer').style.display="none";
  }　
  
document.onkeydown=function()
  {
    var evt=arguments[0] || window.event;
    if (evt.keyCode==27)document.getElementById('meizzDateLayer').style.display="none";
  }　

function IsPinYear(year)            //判断是否闰平年
  {
    if (0==year%4&&((year%100!=0)||(year%400==0))) return true;else return false;
  }　

function GetMonthCount(year,month)  //闰年二月为29天
  {
    var c=MonHead[month-1];if((month==2)&&IsPinYear(year)) c++;return c;
  }　

function GetDOW(day,month,year)     //求某天的星期几
  {
    var dt=new Date(year,month-1,day).getDay()/7; return dt;
  }　

function meizzPrevY()  //往前翻 Year
  {
    if(meizzTheYear > 999 && meizzTheYear <10000){meizzTheYear--;}
    else{alert("年份超出范围（1000-9999）！");}
    meizzSetDay(meizzTheYear,meizzTheMonth);
  }
function meizzNextY()  //往后翻 Year
  {
    if(meizzTheYear > 999 && meizzTheYear <10000){meizzTheYear++;}
    else{alert("年份超出范围（1000-9999）！");}
    meizzSetDay(meizzTheYear,meizzTheMonth);
  }
function meizzToday()  //Today Button
  {
    meizzTheYear = new Date().getFullYear();
    meizzTheMonth = new Date().getMonth()+1;
    meizzSetDay(meizzTheYear,meizzTheMonth);
  }
function meizzPrevM()  //往前翻月份
  {
    if(meizzTheMonth>1){meizzTheMonth--}else{meizzTheYear--;meizzTheMonth=12;}
    meizzSetDay(meizzTheYear,meizzTheMonth);
  }
function meizzNextM()  //往后翻月份
  {
    if(meizzTheMonth==12){meizzTheYear++;meizzTheMonth=1}else{meizzTheMonth++}
    meizzSetDay(meizzTheYear,meizzTheMonth);
  }　

function meizzSetDay(yy,mm)   //主要的写程序**********
{
	meizzTheYear = yy;
	meizzTheMonth = mm;
  meizzWriteHead(yy,mm);
  for (var i = 0; i < 37; i++){meizzWDay[i]=""};  //将显示框的内容全部清空
  var day1 = 1,firstday = new Date(yy,mm-1,1).getDay();  //某月第一天的星期几
  for (var i = firstday; day1 < GetMonthCount(yy,mm)+1; i++){meizzWDay[i]=day1;day1++;}
  for (var i = 0; i < 37; i++)
  { var da = document.getElementById('meizzDay'+i);     //书写新的一个月的日期星期排列
    if (meizzWDay[i]!="")
      { da.innerHTML = "<b>" + meizzWDay[i] + "</b>";
        da.style.backgroundColor = (yy == new Date().getFullYear() &&
        mm == new Date().getMonth()+1 && meizzWDay[i] == new Date().getDate()) ? "#FFD700" : "#ADD8E6";
        da.style.cursor="pointer"
      }
    else{da.innerHTML="";da.style.backgroundColor="";da.style.cursor="default"}
  }
}
function meizzDayClick(n)  //点击显示框选取日期，主输入函数*************
{
  var yy = meizzTheYear;
  var mm = meizzTheMonth;
  if (mm < 10){mm = "0" + mm;}
  if (outObject)
  {
    if (!n) {outObject.value=""; return;}
    if ( n < 10){n = "0" + n;}
    outObject.value= yy + "-" + mm + "-" + n ; //注：在这里你可以输出改成你想要的格式
    closeLayer(); 
    if(typeof(CallbackFunction)=="function")CallbackFunction();
  }
  else {closeLayer(); alert("您所要输出的控件对象并不存在！");}
}
function getEvent()
{
    var   i   =   0;
    if(document.all) return window.event;
    func = getEvent.caller;
    while(func != null)
    {
        var   arg0 = func.arguments[0];
        if(arg0)
        {
            if(arg0.constructor == MouseEvent)
            {
                return   arg0;
            }
        }
        func = func.caller;
    }
    return   null;
}
function testInnerText(t)
{
	if(navigator.appName.indexOf("Explorer") > -1){
		return t.innerText;
	} else{
		return t.textContent;
	}
}
