JS 原始代码时间控件插件







//js时间控件===========================begin==================================================

//js时间控件-动态年份前后50年,调用不同的函数方式会显示不同格式时间

//用法:fPopCalendarTime(this);  (文本域(可编辑)点击事件调用,显示年月日时分2015-03-16 04:10)

//用法2: fPopCalendar(this);(文本域(可编辑)点击事件调用年月日2015-03-16)

//有去除控件尺寸计算,自动让控件匹配大小。

//

//

//函数能适应域值有值或者是无值的域.如果有值可以兼容月份和日期补全零和不补全零。

//初始化值会域中取出,如果值不合法则取当前日期

//日历中日期选中某日会保存值到域中,点击提交也会触发保存操作

//调用函数域值为空,则会使用now的时间、

//当前日期字体设置蓝色,便于用户了解日期。

//function getNowDate()  获取当前时间

//function getlastweekDate() 目标日期?

//function checkD()  提交前检查是是否选择时间

//function fSetDate    提交年月日时分 或年月日

//function HiddenDiv()//控制界面的隐藏

//function fSetSelected(aCell)  //控制年月的选择数据是什么,加法和减法

//function fInitialDate(strDate)//设定年月份时间数目

//function fToggleTags()

//function fTagInBound(aTag)

//function fBuildCal(iYear, iMonth) //函数获取给定日期所有需要显示的日期内容。

//function fGetXY(aTag)  //函数获取某一给定对象的大小坐标

//function fDrawCal 显示星期几

//function fUpdateCal(iYear, iMonth)//函数完成日期文字在页面div中的显示

//function fSetYearMon(iYear, iMon)//调用fSetYearMon函数显示最新的月份内容。

//function fSetYearMonForButton   解决域中有值时按钮点击不能修改月份

//function fPrevMonth()//函数显示上个月的日期。

//function fNextMonth()//函数显示下个月的日期。

//function Point()//函数重新定位坐标。

//function fSetYearMon  //初始化年月

//=========================

//变量声明

var thisform;

var form;

var pathname;

var childwindow = null;



 


//将string转换为unicode

function StrToUnicode(str){

s=new String(str);

unicode="";

for(i=0;i<s.length;i++)

	{

		unicode=unicode+s.charCodeAt(i)+";";

	}

return unicode;

}



//去掉字符串中的空格

function Trim(string)

{

var temp = "";

string = '' + string;

splitstring = string.split(" "); //双引号之间是个空格;

for(i = 0; i < splitstring.length; i++)

temp += splitstring[i];

return temp;

}



var gdCtrl = new Object();

var goSelectTag = new Array();

var gcGray  = "#808080";

var gcToggle = "#FB8664";

var gcBG = "#e5e6ec";

var previousObject = null;

var gdCurDate = new Date();

var giYear = gdCurDate.getFullYear();

var giMonth = gdCurDate.getMonth()+1;

var giDay = gdCurDate.getDate();

//获取用于存储域中获取的值,年、月

var domainValue;

var domainValueYear;

var domainValueMonth;

var domainValueDay;

var gdCurDate1 = new Date();

//timeOrNo判断调用的是那个函数,是否需要显示时分选择

var timeOrNo;

var domainsplit=new Array();	//存放拆分之后的数值

var strMonth="0"; //存放拆分之后的数值-月份

var strDay="0"; //存放拆分之后的数值-日期

var strDayget;

var strDaylog;

var strMinget;

var strMintep;

var strMin;

var strHour;

//domainget用于判断控件的启动次数。

var domainget=1;



function checkD(){

if(nDate.value==""){

alert("请您选取时间")

}else{



fSetDate(nDate.value.substr(0,4),nDate.value.substr(5,2),nDate.value.substr(8,2));

HiddenDiv();

}

}



function fSetDate(iYear, iMonth, iDay){



 if ((iYear == 0) && (iMonth == 0) && (iDay == 0)){

  gdCtrl.value = "";

 }else{



  //alert(iMonth.toString().length)

  if(iMonth.toString().length==1)

  iMonth="0"+iMonth.toString();

  if(iDay.toString().length==1)

  iDay="0"+iDay.toString()

   iMonth=iMonth.toString() ;

    iDay = iDay.toString();

  

  if(gdCtrl.tagName == "INPUT"){

   if(timeOrNo==1){

   

   

    nDate.innerText=iYear+"-"+iMonth+"-"+iDay+" "+iHour.value+":"+iMin.value;

    gdCtrl.value = iYear+"-"+iMonth+"-"+iDay+" "+iHour.value+":"+iMin.value;

  }

  else{

   nDate.innerText=iYear+"-"+iMonth+"-"+iDay;

  gdCtrl.value = iYear+"-"+iMonth+"-"+iDay;

  }

  }else{

    gdCtrl.innerText = iYear+"-"+iMonth+"-"+iDay+" "+iHour.value+":"+iMin.value;

  }

 }

 

 for (i in goSelectTag)

  goSelectTag[i].style.visibility = "visible";

  goSelectTag.length = 0;

  //alert(gdCtrl.value.toString());

 

 window.returnValue=gdCtrl.value.toString();

  //window.close();

}



function stj(){

 VicPopCal.style.visibility = "hidden"; 

}

function HiddenDiv()//控制界面的隐藏

{

var i;

 VicPopCal.style.visibility = "hidden";

    for (i in goSelectTag)

  goSelectTag[i].style.visibility = "visible";

  goSelectTag.length = 0;



}

function fSetSelected(aCell){

 var iOffset = 0;

 var iYear = parseInt(tbSelYear.value);

 var iMonth = parseInt(tbSelMonth.value);

 

 aCell.bgColor = gcBG;

 with (aCell.children["cellText"]){

  var iDay = parseInt(innerText);

  if (color==gcGray)

iOffset = (Victor<10)?-1:1;

iMonth += iOffset;

if (iMonth<1) {

iYear--;

iMonth = 12;

}else if (iMonth>12){

iYear++;

iMonth = 1;

}

 }

 fSetDate(iYear, iMonth, iDay);

if(timeOrNo==2){

      HiddenDiv();

         //年月日下点击日期提交后会关闭控件

   }

}



function Point(iX, iY){

this.x = iX;

this.y = iY;

}



  

function fToggleTags(){

 with (document.all.tags("SELECT")){

 for (i=0; i<length; i++)

 if ((item(i).Victor!="Won")&&fTagInBound(item(i))){

    item(i).style.visibility = "hidden";

goSelectTag[goSelectTag.length] = item(i);

}

 }

}



function fTagInBound(aTag){

 with (VicPopCal.style){

  var l = parseInt(left);

  var t = parseInt(top);

  var r = l+parseInt(width);

  var b = t+parseInt(height);

var ptLT = fGetXY(aTag);

return !((ptLT.x>r)||(ptLT.x+aTag.offsetWidth<l)||(ptLT.y>b)||(ptLT.y+aTag.offsetHeight<t));

 }

}



function fGetXY(aTag){

 var oTmp = aTag;

 var pt = new Point(0,0);

 do {

  pt.x += oTmp.offsetLeft;

  pt.y += oTmp.offsetTop;

  oTmp = oTmp.offsetParent;

 } while(oTmp.tagName!="BODY");

 return pt;

}

function fSetYearMon(iYear, iMon){

//将域中获取的月份赋值给年月的初始化,且月份要有值 



  if((domainget>=1)&&(domainValueMonth>=1)&&(domainValueYear>1800)&&(domainValueMonth<=12)){ 

 //判断当域中时间为不合法值时,不执行赋值。避免域为空时,取到的空值赋值给iMon。

 //domainget>=4表示控件首次使用不触发。可以比较一个表单多个域调用时报错

  iMon=domainValueMonth;

  iYear=domainValueYear;

  

 }



 tbSelMonth.options[iMon-1].selected = true;

 for (i = 0; i < tbSelYear.length; i++)

if (tbSelYear.options[i].value == iYear)

tbSelYear.options[i].selected = true;

 fUpdateCal(iYear, iMon);

}

function fSetYearMonForButton(iYear, iMon){

//月份前后修改按钮,修改年月的功能改到此函数,避免因为有有域值时,无法修改域值。





 tbSelMonth.options[iMon-1].selected = true;

 for (i = 0; i < tbSelYear.length; i++)

if (tbSelYear.options[i].value == iYear)

tbSelYear.options[i].selected = true;

 fUpdateCal(iYear, iMon);

}









//================================================================调用函数=输出年月日时分



//function fPopCalendarTime(popCtrl,dateCtrl,strDate){

function fPopCalendarTime(popCtrl){

 if (popCtrl == previousObject){

  if (VicPopCal.style.visibility == "visible"){

  HiddenDiv();

  return true;

  }

  

 }

  var strDate;

 

 //timeOrNo=1,代表启用的是需要显示时分的函数

 

 timeOrNo=1;

 domainget=domainget+1; 



   //获取参数域的值。获取域中年月,分别存储在全局变量中

	domainValue=popCtrl.value; 

	//alert(domainValue);

	//alert(typeof(domainValue));

	if (domainValue.length <8)

	{

		  //alert("字符串为空,赋值今天的日期");

		 domainValueYear=gdCurDate1.getFullYear();

		 domainValueMonth=gdCurDate1.getMonth()+1;

		 domainValueDay=gdCurDate1.getDate();

		 document.getElementById("iHour").value="08";

		 document.getElementById("iMin").value="30";



	} else{

		//嵌入统一时间格式-开始----------------		

		domainsplit=domainValue.split("-"); //为数据类型拆分值

		//处理月份参数-日期参数

		strMonth=domainsplit[1].toString();

		if (strMonth.length==1)	//alert("月份-参数类型,包含未不全0");

			{		

				strMonth="0"+strMonth;				 

			}	

		strDay=domainsplit[2].toString();//取几号日期,注意小于10时可能带0和不带0

		strDayget=strDay.lastIndexOf(" ");//取空格左侧值,即取日期

		strDaylog=strDay.length-strDayget; //求小时和分钟的长度

		strMinget=strDay.substr(strDayget+1,strDaylog); //strMinget="08-8"	

		strMintep=strMinget.split(":");	

		strDay=strDay.substr(0,strDayget);

		if (strDay.length==1) //("日期-参数类型,包含未不全0");

		{			

			strDay="0"+strDay;

		}

		

		if (strMinget.length>2)//有分钟时才会自动赋值分钟数否则赋值08:00

		{	

			strMin=strMintep[1];

			strHour=strMintep[0];		

			if(strHour.length==1){//补全0

				strHour="0"+strHour;		

			}

			if(strMin.length==1){//补全0

				strMin="0"+strMin;

			}

			 document.getElementById("iHour").value=strHour;

			document.getElementById("iMin").value=strMin;

		 }else{//无分钟时自动补齐

			document.getElementById("iHour").value="08";

			document.getElementById("iMin").value="30";

		 }



		//最后和合成参数

		domainValueYear=domainValue.substr(0,4);

		domainValueMonth=strMonth;

		domainValueDay=strDay;

		//对参数进行格式检查,不合格则使用自动赋值。

		//alert(typeof(domainValueYear));



	//嵌入统一时间格式化结束----------------------------------------------------

	}



 



 

 var myTable= document.getElementById("tr1");//控制是否显示时分的一行

 myTable.style.display="block";//时分一行改为显示

 previousObject = popCtrl;

 gdCtrl = popCtrl;

 fInitialDate(popCtrl);

 fSetYearMon(giYear, giMonth); 

 var point = fGetXY(popCtrl);

 

 

 with (VicPopCal.style) {

  left = point.x;

top = point.y+popCtrl.offsetHeight;

width = VicPopCal.offsetWidth;

//width = 210; //

height = VicPopCal.offsetHeight;

fToggleTags(point); 

visibility = 'visible';

 }

}



//==============================================================================调用控件函数年月日

function fPopCalendar(popCtrl){

    var strDate;

	if (popCtrl == previousObject){

		if (VicPopCal.style.visibility == "visible"){

			HiddenDiv();

	      return true;

	    } 

	 }

   var myTable= document.getElementById("tr1");

   myTable.style.display="none";//控制是否显示时分秒的一行

   timeOrNo=2;

  domainget=domainget+1;



   //获取参数域的值。获取域中年月日,分别存储在全局变量中

	domainValue=popCtrl.value; 

	//alert(domainValue);

	//alert(typeof(domainValue));

	if (domainValue.length<8)

	{

	  //alert("字符串为空,赋值今天的日期");

	 domainValueYear=gdCurDate1.getFullYear();

	 domainValueMonth=gdCurDate1.getMonth()+1;

	 domainValueDay=gdCurDate1.getDate();



	} else{

		// alert("字符串不为空,使用手动输入的值");

		//嵌入时间格式-----------------标准化时间格式为如:2016-01-01-------------------

		domainsplit=domainValue.split("-"); //为数据类型拆分值	

		strMonth=domainsplit[1].toString();//处理月份参数

		if (strMonth.length==1)

		{	

			strMonth="0"+strMonth;		

		}	

		strDay=domainsplit[2].toString();		//处理日期参数

		if (strDay.length==1) //	alert("日期-参数类型,包含未不全0");

		{		

			strDay="0"+strDay;

		}

		//最后和合成参数

		domainValueYear=domainValue.substr(0,4);

		domainValueMonth=strMonth;

		domainValueDay=strDay;

	//嵌入时间格式化结束----------------------------------------------------

	}

  

	previousObject = popCtrl;

	gdCtrl = popCtrl;

	fInitialDate(popCtrl);

	fSetYearMon(giYear, giMonth); 

	var point = fGetXY(popCtrl);//找出域的坐标

 

	with (VicPopCal.style) {

		left = point.x;

		top = point.y+popCtrl.offsetHeight;

		width = VicPopCal.offsetWidth;

		//width = 210; //

		height = VicPopCal.offsetHeight;

		fToggleTags(point); 

		visibility = 'visible';

	}

}





//

// Added by Han Chen

function fInitialDate(strDate){

if( strDate == null || strDate.length != 10 )

return false;



var sYear = strDate.substring(0,4);

var sMonth = strDate.substring(5,7);

var sDay  = strDate.substring(8,10);



if( sMonth.charAt(0) == '0' ) { sMonth = sMonth.substring(1,2); }

if( sDay.charAt(0)  == '0' ) { sDay  = sDay.substring(1,2);  }



var nYear = parseInt(sYear );

var nMonth = parseInt(sMonth);

var nDay  = parseInt(sDay );



if ( isNaN(nYear ) ) return false;

if ( isNaN(nMonth) ) return false;

if ( isNaN(nDay ) ) return false;



var arrMon = new Array(12);

arrMon[ 0] = 31; arrMon[ 1] = nYear % 4 == 0 ? 29:28;

arrMon[ 2] = 31; arrMon[ 3] = 30;

arrMon[ 4] = 31; arrMon[ 5] = 30;

arrMon[ 6] = 31; arrMon[ 7] = 31;

arrMon[ 8] = 30; arrMon[ 9] = 31;

arrMon[10] = 30; arrMon[11] = 31;



if ( nMonth < 1 || nMonth > 12 ) return false;

if ( nDay < 1 || nDay > arrMon[nMonth - 1] ) return false;



giYear = nYear;

giMonth = nMonth;

giDay  = nDay;

return true;

}

  



function fBuildCal(iYear, iMonth) {

 var aMonth=new Array();

 for(i=1;i<7;i++)

  aMonth[i]=new Array(i);

 

 var dCalDate=new Date(iYear, iMonth-1, 1);

 var iDayOfFirst=dCalDate.getDay();

 var iDaysInMonth=new Date(iYear, iMonth, 0).getDate();

 var iOffsetLast=new Date(iYear, iMonth-1, 0).getDate()-iDayOfFirst+1;

 var iDate = 1;

 var iNext = 1;



 for (d = 0; d < 7; d++)

aMonth[1][d] = (d<iDayOfFirst)?-(iOffsetLast+d):iDate++;

 for (w = 2; w < 7; w++)

  for (d = 0; d < 7; d++)

aMonth[w][d] = (iDate<=iDaysInMonth)?iDate++:-(iNext++);

 return aMonth;

}



function fDrawCal(iYear, iMonth, iCellHeight, sDateTextSize) {

 var WeekDay = new Array("日","一","二","三","四","五","六");

 var styleTD = " bgcolor='"+gcBG+"' bordercolor='"+gcBG+"' valign='middle' align='center' height='"+iCellHeight+"' style='font:bold arial "+sDateTextSize+";";     







 with (document) {

write("<tr>");

for(i=0; i<7; i++){

write("<td "+styleTD+"color:maroon' >"+ WeekDay[i] + "</td>");

}

write("</tr>");



  for (w = 1; w < 7; w++) {

write("<tr>");

for (d = 0; d < 7; d++) {

write("<td id=calCell "+styleTD+"cursor:hand;' onMouseOver='this.bgColor=gcToggle' onMouseOut='this.bgColor=gcBG' onclick='fSetSelected(this)'>");

write("<font id=cellText Victor='Hcy_Flag'> </font>"); 

write("</td>")

}

write("</tr>");

}

 }

}



function fUpdateCal(iYear, iMonth) {

 myMonth = fBuildCal(iYear, iMonth);

 var i = 0;

 //alert(myMonth[1][1]);

// alert(myMonth[2][2]);

 //alert(myMonth[3][3]);

 for (w = 0; w < 6; w++)

 for (d = 0; d < 7; d++)

 with (cellText[(7*w)+d]) {

     Victor = i++;

     if (myMonth[w+1][d]<0) {

        color = gcGray;

        innerText = -myMonth[w+1][d];

     }else{

        color = ((d==0)||(d==6))?"red":"black";

        innerText = myMonth[w+1][d];

      }

      //日期等于当前日期时,将那天日期颜色修改为深色。表示是今天

      if(myMonth[w+1][d]==domainValueDay)

       color="blue";

 }

}







function fPrevMonth(){

 var iMon = tbSelMonth.value;

 var iYear = tbSelYear.value;

 

 if (--iMon<1) {

 iMon = 12;

 iYear--;

 }

 

 fSetYearMonForButton(iYear, iMon);

}



function fNextMonth(){

 var iMon = tbSelMonth.value;

 var iYear = tbSelYear.value;

 

 if (++iMon>12) {

 iMon = 1;

 iYear++;

 }

 

 fSetYearMonForButton(iYear, iMon);

}

  



var gMonths = new Array("一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月");



with (document) {

write("<Div id='VicPopCal' style='POSITION:absolute;VISIBILITY:hidden;border:2px ridge;z-index:100;'>");

write("<table border='0' bgcolor='#cccccc'>");

write("<TR>");

write("<td valign='middle' align='center'><input type='button' name='PrevMonth' value='<' style='height:20;width:20;FONT:bold' onClick='fPrevMonth()'>");

write(" <SELECT name='tbSelYear' onChange='fUpdateCal(tbSelYear.value, tbSelMonth.value)' Victor='Won'>");

var myDate = new Date();  

var stayear=myDate.getFullYear()-50;

var endyear=myDate.getFullYear()+50;

for(i=stayear;i<endyear;i++)

write("<OPTION value='"+i+"'>"+i+" 年</OPTION>");

write("</SELECT>");

write(" <select name='tbSelMonth' onChange='fUpdateCal(tbSelYear.value, tbSelMonth.value)' Victor='Won'>");

for (i=0; i<12; i++)

write("<option value='"+(i+1)+"'>"+gMonths[i]+"</option>");

write("</SELECT>");

write(" <input type='button' name='PrevMonth' value='>' style='height:20;width:17;FONT:bold' onclick='fNextMonth()'>");

write("</td>");

write("</TR><TR>");

write("<td align='center'>");

write("<DIV style='background-color:teal'><table width='100%' border='0'>");

fDrawCal(giYear, giMonth, 20, '12');

write("</table></DIV>");

write("</td>");

write("</TR><TR><TD align='center'>");

//起一行显示小时和分钟

write("<TABLE width='100%'><TR id='tr1'><TD colspan=2 align='center'>");

write("时间:<select Victor='Won' name=iHour>")

var j

for (i=0; i<24; i++){

if(i<10){

j="0"+i

}else{

j=i;

};

write("<option value='"+j+"'>"+j+"</option>");

}

write("</SELECT>小时");

write("<select Victor='Won' name=iMin>")

for (i=00; i<60; i++){

if(i<10){

k="0"+i

}else{

k=i;

};

write("<option value='"+k+"'>"+k+"</option>");

}

write("</SELECT>分钟");

write("</td></TR>");

write("<TR><th>");

write("<input name=nDate type=hidden><B style='cursor:hand;width:50%'  onMouseOver='this.style.color=gcToggle' onMouseOut='this.style.color=0' onclick='checkD()'>提交</b>");

write("</th><th Text-align:center><B style='cursor:hand;width:50%' onclick='HiddenDiv()' onMouseOver='this.style.color=gcToggle' onMouseOut='this.style.color=0'>关闭</B>");

write("");



write("</th></tr></table>");



write("</TD></TR>");

write("</TABLE></Div>");

}



//js时间控件===========================end==================================================



//4.打印操作

function print()

{

 var fso, tf;

 var pathname = (window.location.pathname);

 var href = (window.location.href);

 thisform = document.forms[0]; 

 fso = new ActiveXObject("Scripting.FileSystemObject");

tmpFolder = fso.GetSpecialFolder(2)+"\\打印文件.doc";

alert(tmpFolder);

 tf = fso.CreateTextFile(tmpFolder, true);   

tf.WriteLine("<html>");

tf.WriteLine("<head>");

tf.WriteLine("<title>");

tf.WriteLine("发文打印");

tf.WriteLine("</title>");

tf.WriteLine("</head>");

tf.WriteLine("<body>");

tf.WriteLine(document.all.quanwen_html.innerHTML);

//tf.WriteLine("<p align='left'>");

//tf.WriteLine(document.all.allbody.innerHTML);

//tf.WriteLine(body); //正文

//tf.WriteLine("</p>")

tf.WriteLine("</body>");

tf.WriteLine("</html>"); 

tf.Close();

var  doc = new ActiveXObject("Word.Application");

doc.Visible = true;

doc.Documents.Open (tmpFolder); 

}








//用法:fPopCalendarTime(this);  (文本域(可编辑)点击事件调用,显示年月日时分2015-03-16 04:10)
//用法2: fPopCalendar(this);(文本域(可编辑)点击事件调用年月日2015-03-16)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值