ASP+JavaScript的完整的日历使用

来源:互联网 发布:windows自带脚本编程 编辑:程序博客网 时间:2024/06/05 06:02
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

第一步:保存下列文件为:calendar.ASP

<%@ language = vbscript %>
<% option explicit %>
<%
function sfont(isize,sface,scolor,bscale)
 dim soutputstr
 soutputstr = "<font"
 if sface <> "" then
   soutputstr = soutputstr & " face='" & sface & "'"
 else
   soutputstr = soutputstr & " face='helv'"
 end if

 if isize = "" then
  isize = 1
 end if
 if bscale then
  isize = cint(isize * 1)
 end if
 soutputstr = soutputstr & " size=" & isize
 if scolor <> "" then
   soutputstr = soutputstr & " color=" & scolor
 end if
 
 soutputstr = soutputstr & ">"

 sfont = soutputstr
end function
on error resume next
dim datecntrl, dy, mo, yr, nextmonth, startwith, thismo, thisyr, thisdate,the_type
dim prevmonthlastdate, currmonthlastdate, i, j, lastnum, anydate,default_value

datecntrl= request("object")
default_value=request("value")
the_type=request("type")
if the_type<>"datetime" then
 the_type="date"
end if

if default_value="" then
 yr = year(date)
 mo = month(date)
 dy = day(date)
else
    dim pos1
    dim deal_value
    deal_value=default_value
    pos1=instr(deal_value,"-")
 yr = cint(mid(deal_value,1,pos1-1))
 deal_value=mid(deal_value,pos1+1)
 pos1=instr(deal_value,"-")
 mo = cint(mid(deal_value,1,pos1-1))
 if trim(the_type)="date" then
  dy = cint(mid(deal_value,pos1+1))
 else
     dim h,m,s
  deal_value=mid(deal_value,pos1+1)
  pos1=instr(deal_value," ")
     dy=cint(mid(deal_value,1,pos1-1))
  deal_value=mid(deal_value,pos1+1)
  pos1=instr(deal_value,":")
     h=cint(mid(deal_value,1,pos1-1))
  deal_value=mid(deal_value,pos1+1)
  pos1=instr(deal_value,":")
     m=cint(mid(deal_value,1,pos1-1))
    s=cint(mid(deal_value,pos1+1))
 end if 
end if

nextmonth = false
%>
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=gb2312">
  <meta name="generator" content="microsoft frontpage 4.0">
  <meta name="progid" content="frontpage.editor.document">
  <style type="text/css">
 a
  { font-size: 13px; color: #000000;text-decoration: none }
 a:hover
 {color: #ff0000;
 }
  </style>
  <title>日历</title>
  <script language="javascript">
 //检查字符串是否为日期,返回值:false、true
function f_chkdate(datestr)
{
 var lthdatestr
 if (datestr != "")
  lthdatestr= datestr.length ;
 else
  lthdatestr=0;
  
 var tmpy="";
 var tmpm="";
 var tmpd="";
 //var datestr;
 var status;
 status=0;
 if ( lthdatestr== 0)
  return false;

    if(lthdatestr>10)
       return false;
 
 for (i=0;i < lthdatestr;i++)
 { if (datestr.charat(i)== '-')
  {
   status++;
  }
  if (status>2)
  {
   //alert("invalid format of date!");
   return false;
  }
  if ((status==0) && (datestr.charat(i)!='-'))
  {
   tmpy=tmpy+datestr.charat(i)
  }
  if ((status==1) && (datestr.charat(i)!='-'))
  {
   tmpm=tmpm+datestr.charat(i)
  }
  if ((status==2) && (datestr.charat(i)!='-'))
  {
   tmpd=tmpd+datestr.charat(i)
  }

 }
 year=new string (tmpy);
 month=new string (tmpm);
 day=new string (tmpd);
 
 //tempdate= new string (year+month+day);
 //alert(tempdate);
 if ((tmpy.length!=4) || (tmpm.length>2) || (tmpd.length>2))
 {
  //alert("invalid format of date!");
  return false;
 }
 if (!((1<=month) && (12>=month) && (31>=day) && (1<=day)) )
 {
     //alert ("invalid month or day!");
  return false;
 }
 if (!((year % 4)==0) && (month==2) && (day==29))
 {
  //alert ("this is not a leap year!");
  return false;
 }
 if ((month<=7) && ((month % 2)==0) && (day>=31))
 {
  //alert ("this month is a small month!");
  return false;
 
 }
 if ((month>=8) && ((month % 2)==1) && (day>=31))
 {
  //alert ("this month is a small month!");
  return false;
 }
 if ((month==2) && (day==30))
 {
  //alert("the febryary never has this day!");
  return false;
 }
 
 return true;
}


  function right(str,number)
  {
   return str.substr(str.length - number,str.length);
  }
  function setdate(dy,mo,yr,vbool)
  { 
            if (vbool)
               {
               if (mo<10)  mo="0"+mo;
               if (dy<10)  dy="0"+dy;
   
   <%if the_type="datetime" then%>
    top.opener.<%= datecntrl %>.value =yr+"-"+mo+"-"+dy+" "+userform.hour.value+":"+userform.minute.value+":"+userform.second.value;
   <%else%>
    top.opener.<%= datecntrl %>.value =yr+"-"+mo+"-"+dy;
   <%end if%>
   top.window.close();
              
               }
  
      <%if the_type="datetime" then%>
       self.location.href="calendar.ASP?type=<%=the_type%>&object=<%= datecntrl %>&value="+yr+"-"+right(("0"+mo),2)+"-"+right(("0"+dy),2)+" "+userform.hour.value+":"+userform.minute.value+":"+userform.second.value;
   <%else%>
    self.location.href="calendar.ASP?type=<%=the_type%>&object=<%= datecntrl %>&value="+yr+"-"+right(("0"+mo),2)+"-"+right(("0"+dy),2);
   <%end if%>
  }
  
  function savedate()
  {
   <%if the_type="datetime" then%>
    top.opener.<%= datecntrl %>.value ="<%= yr & "-" & right("0"+cstr(mo),2) & "-" & right("0"+cstr(dy),2) %>"+" "+userform.hour.value+":"+userform.minute.value+":"+userform.second.value;
   <%else%>
    top.opener.<%= datecntrl %>.value ="<%= yr & "-" & right("0"+cstr(mo),2) & "-" & right("0"+cstr(dy),2) %>";
   <%end if%>
   top.window.close();
  }
  
  </script>
 </head>
 <body onload="javascript:window.focus()" bgcolor="#ccffff">
  <form name="userform">
   <table width="100%" cellpadding="2" cellspacing="0">
    <tr>
     <td align="left">
      <%= sfont("2","宋体","",true) %>
      <%= writeyears(yr) %>
     </td>
     <td align="right">
      <%= sfont("2","宋体","",true) %>
      <%= writemonths(mo) %>
     </td>
    </tr>
   </table>
   <table width="100%" border="1" bordercolor="silver" cellpadding="2" cellspacing="0">
    <%

 function nextdate(startwith, maxdays)
  startwith = startwith + 1
  if startwith > maxdays then
   startwith = 1
  end if
  
  nextdate = startwith
 end function
 
 function getlastday(mo,yr)
    if mo=2 then
   if (yr mod 4)=0 then
       getlastday = 29
   else
    getlastday = 28
   end if
  elseif ((mo = 0) or (mo = 1) or (mo = 3) or (mo = 5) or (mo = 7) or (mo = 8) or (mo = 10) or (mo = 12)) then
      getlastday =  31
  else
      getlastday =  30
  end if
   end function
 
 function getfirstdayoffset(mo,yr)
  getfirstdayoffset = weekday(mo & "/01/" & yr)-1
   end function
 
 function writemonths(selmo)
  dim i, selstr
  selstr = "<select name='months' onchange='setdate(" & dy & ",this.selectedindex+1," & yr & ",false);'>"
  for i=1 to 12
   if selmo = i then
    selstr = selstr & "<option selected>" & monthname(i)   
   else
    selstr = selstr & "<option>" & monthname(i)
   end if
  next                
  selstr = selstr & "</select>"
  writemonths = selstr
 end function
 
 function writeyears(selyear)
  dim i, selstr
  selstr = "<select name='years' onchange='setdate(" & dy & "," & mo & ",this.options[this.selectedindex].value,false);'>"
  for i=1900 to 2100
   if selyear = i then
    selstr = selstr & "<option selected value=" & i & ">" & i & "年"   
   else
    selstr = selstr & "<option value=" & i & ">" & i & "年"
   end if
  next                
  selstr = selstr & "</select>"
  writeyears = selstr  
 end function

 prevmonthlastdate=getlastday((mo-1),yr)  
 currmonthlastdate=getlastday(mo,yr) 
 startwith=(prevmonthlastdate-getfirstdayoffset(mo, yr))

%>
    <tr>
     <td width="20" height="20" bgcolor="gray" bordercolor="silver" bordercolordark="#ffffff" bordercolorlight="#000000">
      <%= sfont("2","宋体","#ffffff",true) %>
      <b>日</b>
     </td>
     <td width="20" height="20" bgcolor="gray" bordercolor="silver" bordercolordark="#ffffff" bordercolorlight="#000000">
      <%= sfont("2","宋体","#ffffff",true) %>
      <b>一</b>
     </td>
     <td width="20" height="20" bgcolor="gray" bordercolor="silver" bordercolordark="#ffffff" bordercolorlight="#000000">
      <%= sfont("2","宋体","#ffffff",true) %>
      <b>二</b>
     </td>
     <td width="20" height="20" bgcolor="gray" bordercolor="silver" bordercolordark="#ffffff" bordercolorlight="#000000">
      <%= sfont("2","宋体","#ffffff",true) %>
      <b>三</b>
     </td>
     <td width="20" height="20" bgcolor="gray" bordercolor="silver" bordercolordark="#ffffff" bordercolorlight="#000000">
      <%= sfont("2","宋体","#ffffff",true) %>
      <b>四</b>
     </td>
     <td width="20" height="20" bgcolor="gray" bordercolor="silver" bordercolordark="#ffffff" bordercolorlight="#000000">
      <%= sfont("2","宋体","#ffffff",true) %>
      <b>五</b>
     </td>
     <td width="20" height="20" bgcolor="gray" bordercolor="silver" bordercolordark="#ffffff" bordercolorlight="#000000" style="font-family: helv,arial; font-size: 10pt;">
      <%= sfont("2","宋体","#ffffff",true) %>
      <b>六</b>
     </td>
    </tr>
    <% for j = 1 to 6 %>
    <tr>
     <% for i = 1 to 7 %>
     <% if j = 1 then %>
     <% startwith = nextdate(startwith, prevmonthlastdate) %>
     <% if startwith = dy and startwith < 7 then %>
     <td width="20" height="20" bgcolor="#aaaaaa" bordercolor="#aaaaaa" bordercolordark="#ffffff" bordercolorlight="#aaaaaa">
      <% else %>
     <td width="20" height="20" bordercolor="silver" bordercolordark="#ffffff" bordercolorlight="#000000">
      <% end if %>
      <% thisyr = yr %>
      <%= sfont("2","宋体","",true) %>
      <% if startwith < 7 then %>
      <b>
       <% thismo = mo %>
       <% else %>
       <% thismo = mo - 1 %>
       <% if thismo = 0 then %>
       <% thismo = 12 %>
       <% thisyr = yr-1 %>
       <% end if %>
       <% end if %>
       <a href="javascript:setdate( <%= startwith %>, <%= thismo %>,<%= thisyr %>,false);" >
        <% response.write startwith %>
       </a></b>
     </td>
     <% else %>
     <% lastnum = startwith %>
     <% startwith = nextdate(startwith, currmonthlastdate) %>
     <% if lastnum > startwith then%>
     <% nextmonth = true %>
     <% end if %>
     <% if startwith = dy and not nextmonth then %>
     <td width="20" height="20" bgcolor="#aaaaaa" bordercolor="#aaaaaa" bordercolordark="#aaaaaa" bordercolorlight="#ffffff">
      <% else %>
     <td width="20" height="20" bordercolor="silver" bordercolordark="#ffffff" bordercolorlight="#000000">
      <% end if %>
      <% thisyr = yr %>
      <% if not nextmonth then %>
      <b>
       <% thismo = mo %>
       <% else %>
       <% thismo = mo + 1 %>
       <% if thismo = 13 then %>
       <% thismo = 1 %>
       <% thisyr = yr+1 %>
       <% end if %>
       <% end if %>
       <%= sfont("2","宋体","",true) %>
       <a href="javascript:setdate(<%= startwith %>, <%= thismo %>, <%= thisyr %>,false);" >
        <% response.write startwith %>
       </a></b>
     </td>
     <% end if %>
     <% next %>
    </tr>
    <% next %>
   </table>
   <table>
    <tr>
     <td>
      <%if the_type="datetime" then%>
      <select size="1" name="hour">
       <%
  for i=0 to 23
    if (i=h) then
       response.write "<option selected value='"+right("0"+cstr(i),2)+"'>"+right("0"+cstr(i),2)+"时"+"</option>"
    else
       response.write "<option value='"+right("0"+cstr(i),2)+"'>"+right("0"+cstr(i),2)+"时"+"</option>"
    end if
  next
  %>
      </select>
      <select size="1" name="minute">
       <%
  for i=0 to 59
    if (i=m) then
       response.write "<option selected value='"+right("0"+cstr(i),2)+"'>"+right("0"+cstr(i),2)+"分"+"</option>"
    else  
       response.write "<option value='"+right("0"+cstr(i),2)+"'>"+right("0"+cstr(i),2)+"分"+"</option>"
    end if  
  next
  %>
      </select>
      <select size="1" name="second">
       <%
  for i=0 to 59
    if (i=s) then
       response.write "<option selected value='"+right("0"+cstr(i),2)+"'>"+right("0"+cstr(i),2)+"秒"+"</option>"
    else
       response.write "<option value='"+right("0"+cstr(i),2)+"'>"+right("0"+cstr(i),2)+"秒"+"</option>"
    end if      
  next
  %>
      </select>
      <%end if%>
     </td>
    </tr>
   </table>
   <center>
    <input type="button" id="ok" value=" 确定 " onclick="savedate();">&nbsp;&nbsp; <input type="button" value=" 取消 " onclick="top.window.close();">
   </center>
  </form>
  <script language="javascript">
var strdate = <%=yr%>+"-"+right(("0"+<%=mo%>),2)+"-"+right(("0"+<%=dy%>),2);
if (f_chkdate(strdate))
 document.all.ok.disabled = false;
else
 document.all.ok.disabled = true;
  </script>
 </body>
</html>

 

第二步:保存下列文件为:javascriptdate.js


function f_get_date(object_name){
 var object_value="";
 eval("object_value="+object_name+".value");
if(!f_chkdate(object_value)){
  var v_today=new date();
  object_value=v_today.getyear()+"-"+(v_today.getmonth()+1)+"-"+(v_today.getdate());
 }
open('calendar.ASP?type=date&object='+object_name+'&value='+object_value,'calendar','resizable=yes,toolbar=no,scrollbars=no,directories=no,menubar=no,width=180,height=240');
}
//获取日历时间函数
function f_get_datetime(object_name){
 var object_value="";
 eval("object_value="+object_name+".value");
 open('calendar.ASP?type=datetime&object='+object_name+'&value='+object_value,'calendar','resizable=yes,toolbar=no,scrollbars=no,directories=no,menubar=no,width=210,height=270');
}


//检查字符串是否为日期,返回值:false、true
function f_chkdate(datestr)
{
 var lthdatestr
 if (datestr != "")
  lthdatestr= datestr.length ;
 else
  lthdatestr=0;
  
 var tmpy="";
 var tmpm="";
 var tmpd="";
 //var datestr;
 var status;
 status=0;
 if ( lthdatestr== 0)
  return false;

    if(lthdatestr>10)
       return false;
 
 for (i=0;i < lthdatestr;i++)
 { if (datestr.charat(i)== '-')
  {
   status++;
  }
  if (status>2)
  {
   //alert("invalid format of date!");
   return false;
  }
  if ((status==0) && (datestr.charat(i)!='-'))
  {
   tmpy=tmpy+datestr.charat(i)
  }
  if ((status==1) && (datestr.charat(i)!='-'))
  {
   tmpm=tmpm+datestr.charat(i)
  }
  if ((status==2) && (datestr.charat(i)!='-'))
  {
   tmpd=tmpd+datestr.charat(i)
  }

 }
 year=new string (tmpy);
 month=new string (tmpm);
 day=new string (tmpd);
 
 //tempdate= new string (year+month+day);
 //alert(tempdate);
 if ((tmpy.length!=4) || (tmpm.length>2) || (tmpd.length>2))
 {
  //alert("invalid format of date!");
  return false;
 }
 if (!((1<=month) && (12>=month) && (31>=day) && (1<=day)) )
 {
     //alert ("invalid month or day!");
  return false;
 }
 if (!((year % 4)==0) && (month==2) && (day==29))
 {
  //alert ("this is not a leap year!");
  return false;
 }
 if ((month<=7) && ((month % 2)==0) && (day>=31))
 {
  //alert ("this month is a small month!");
  return false;
 
 }
 if ((month>=8) && ((month % 2)==1) && (day>=31))
 {
  //alert ("this month is a small month!");
  return false;
 }
 if ((month==2) && (day==30))
 {
  //alert("the febryary never has this day!");
  return false;
 }
 
 return true;
}


第三步:在页中加入如下示例:(使用页)

        <script language="javascript" src="javascriptdate.js"></script>

        <input class="input" style="cursor:hand"; name="mytime" type="text" id="mytime" size="16" value="" readonly="true">

    1.获取日期:
        <script language="javascript" event="onclick" for="mytime">
                f_get_date("document.all.mytime");
        </script>
       
     2.获取日期和时间
         <script language="javascript" event="onclick" for="mytime">
                f_get_datetime("document.all.mytime");
         </script>


<<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击