A commonly used JS time and date processing function

Source: Internet
Author: User
Tags getdate tagname time and date trim
The code is as follows Copy Code

var m_idatepickercount=0;
var cl_dpmaxyear=9999;
var cl_dpmaxmonth=11;
var cl_dpmaxday=31;
var cl_dpminyear=1600;
var cl_dpminmonth=0;
var Cl_dpminday=1;

Get Current date
var t_today = new Date ();
var t_now = T_today.getdate ();
var t_year = T_today.getyear ();
if (T_year <) T_year + + 1900; Y2K fix
var t_month = T_today.getmonth () +1;
var Monarr = new Array (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
if ((t_year% 4 = 0) && (t_year% 100!= 0)) | | (t_year% 400 = 0)) MONARR[1] = "29";

function Createdatepicker (txtname,lyear,lmonth,lday)


{


var temp_today = new Date ();


if (lyear==null) lyear=temp_today.getyear ();


if (lmonth==null) lmonth=temp_today.getmonth () +1;


if (lday==null) lday=temp_today.getdate ();


var dpid= "Dp_" + (m_idatepickercount++);


var dt=dp_getvaliddate (Lyear,lmonth,lday);


var dpname = "Dpn_" + txtname;


if (dt==null)


Dt=new Date ();


document.write ("&lt;span class=dpframe id=" +dpid+ "name=" +dpname+ "&gt;");


document.write ("&lt;input class=dpyear type=text value=" +dt.getfullyear () + "size=4 maxlength=4 onfocus=" return dp_ Focus (' year '), "onblur=" Return Dp_blur (' year '), "onkeypress=" return Keyfilter (' number '), "onkeydown=" return DP_ KeyDown (' year '); " &gt; ");


document.write ("&lt;font class=dpyeardes&gt; year &lt;/font&gt;");


document.write ("&lt;input class=dpmonth type=text value=" + (Dt.getmonth () +1) + "size=1 maxlength=2 onfocus=" return dp_ Focus (' month '), "onblur=" return Dp_blur (' month '), "onkeypress=" return Keyfilter (' number '), "onkeydown=" return DP_ KeyDown (' month '); " &gt; ");


document.write ("&lt;font class=dpmonthdes&gt; month &lt;/font&gt;");


document.write ("&lt;input class=dpday type=text value=" +dt.getdate () + "size=1 maxlength=2 onfocus=" return Dp_focus (' Day '), "onblur=" return Dp_blur (' Day '), "onkeypress=" return Keyfilter (' number '), "onkeydown=" return Dp_keydown (' Day ') ;" &gt; ");


document.write ("&lt;font class=dpdaydes&gt; Day &lt;/font&gt;");


document.write ("&lt;span class=dpsep&gt;&lt;/span&gt;");


document.write ("&lt;a class=dpdropbtn href=" "onclick=" Dp_dropclick (); return false; "title=" Select Date "&gt;?&lt;/a&gt;");


if (typeof (txtname) = = "string" &amp;&amp; txtname.length&gt;0)


{


document.write ("&lt;input type=hidden value= '" +dt.format ("yyyy/mm/dd") + "' Name=" +txtname+ "&gt;");


}


document.write ("&lt;/span&gt;");

var dp=document.all (dpid);
Dp_initdatepicker (DP,DT);
return DP;
}

function Dp_getvaliddate (lyear,lmonth,lday)
{
var dt=new Date ();
if (Lyear==null | | isNaN (parseint (lyear,10))
Lyear=dt.getfullyear ();
Else
Lyear=parseint (lyear,10);

if (Lmonth==null | | isNaN (parseint (lmonth,10))
Lmonth=dt.getmonth ();
Else
Lmonth=parseint (lmonth,10)-1;

if (Lday==null | | isNaN (parseint (lday,10))
Lday=dt.getdate ();
Else
Lday=parseint (lday,10);

Dt=new Date (Lyear,lmonth,lday);
var cdmax=new Date (Cl_dpmaxyear,cl_dpmaxmonth,cl_dpmaxday);
var cdmin=new Date (Cl_dpminyear,cl_dpminmonth,cl_dpminday);
if (Dt.compare (cdmax) >0 | | dt.compare (cdmin) <0)
Dt=null;
return DT;
}

function Dp_initdatepicker (DP,DT)
{
if (DP)
{
Private Property
DP.CURDATE=DT;
Dp.dpenabled=true;
Dp.maxday=cl_dpmaxday;
Dp.maxmonth=cl_dpmaxmonth;
Dp.maxyear=cl_dpmaxyear;
Dp.minday=cl_dpminday;
Dp.minmonth=cl_dpminmonth;
Dp.minyear=cl_dpminyear;
Dp.olddate=dt.clone ();

Private method


dp.getdropdowntable=dp_getdropdowntable;


Dp.getmonthname=dp_getmonthname;


Dp.hidedropdown=dp_hidedropdown;


Dp.initdropdown=dp_initdropdown;


Dp.ondatechange=dp_ondatechange;


Dp.refreshposttext=dp_refreshposttext;


Dp.showdropdown=dp_showdropdown;





Public property


All Span Properties can is used;


dp.offsethor=0;





Public method


Dp.setfocus=dp_setfocus;


Dp.format=dp_format;


Dp.getdatecontent=dp_getdatecontent;


Dp.getday=dp_getday;


dp.getenabled=dp_getenabled;


Dp.getmonth=dp_getmonth;


Dp.getyear=dp_getyear;


Dp.refreshview=dp_refreshview;


Dp.setaccesskey=dp_setaccesskey;


Dp.setcurdate=dp_setcurdate;


Dp.setnoborder=dp_setnoborder;


Dp.setdatedes=dp_setdatedes;


dp.setenabled=dp_setenabled;


Dp.setformat=dp_setformat;


Dp.setmaxdate=dp_setmaxdate;


Dp.setmindate=dp_setmindate;


Dp.settabindex=dp_settabindex;


Dp.setweekname=dp_setweekname;





Event


Dp.datechanged=null;





Init View


Dp.refreshview ();


}


}

function Dp_createdropdown ()


{


var ddt=getdropdowntable ();


if (DDT)


return to DDT;


Document.body.insertAdjacentHTML ("BeforeEnd",


"&lt;table id=dpdropdowntable cellspacing=0" +


"Onclick=" Dp_ddt_click (); "+


"Ondblclick=" Dp_ddt_dblclick (); &gt; "+


"&lt;tr class=dptitle&gt;" +


"&lt;td colspan=2 align=left&gt;&lt;span class=dpbtn onclick=" Dp_monthchange ( -12); "title=" last year &gt; previous year &lt;/span&gt; "+


"&amp;nbsp;&lt;span class=dpbtn onclick=" Dp_monthchange ( -1); "Title=" Last month "&gt; last month &lt;/span&gt;&lt;/TD&gt;" +


"&lt;td align=center class=dptitle colspan=3&gt;&lt;/td&gt;" +


"&lt;td colspan=2 align=right&gt;&lt;span class=dpbtn onclick=" Dp_monthchange (1); "title=" next month &gt; next month &lt;/span&gt; &amp;nbsp; " +


"&lt;span class=dpbtn onclick=" Dp_monthchange (a); "title=" next year "&gt; next year &lt;/span&gt;&lt;/TD&gt;" +


"&lt;/TR&gt;" +


"&lt;TR&gt;" +


"&lt;td class=dpweekname&gt; Sunday &lt;/TD&gt;" +


"&lt;td class=dpweekname&gt; Monday &lt;/TD&gt;" +


"&lt;td class=dpweekname&gt; Tuesday &lt;/TD&gt;" +


"&lt;td class=dpweekname&gt; Wednesday &lt;/TD&gt;" +


"&lt;td class=dpweekname&gt; Thursday &lt;/TD&gt;" +


"&lt;td class=dpweekname&gt; Friday &lt;/TD&gt;" +


"&lt;td class=dpweekname&gt; Saturday &lt;/TD&gt;" +


"&lt;/TR&gt;" +


"&lt;/TABLE&gt;");


Ddt=getdropdowntable ();


if (DDT)


{


var row=null;


var cell=null;


for (var i=2; i&lt;8; i++)


{


Row=ddt.insertrow (i);


if (row)


{


for (var j=0; j&lt;7; J + +)


{


Cell=row.insertcell (j);


if (cell)


//     {


//     }


}


}


}


}


if (ddt.rows.length!=8)


Ddt=null;


return to DDT;


}

function Dp_getyear ()
{
var dp=this;
return Dp.curDate.getFullYear ();
}

function Dp_getmonth ()
{
var dp=this;
Return Dp.curDate.getMonth () +1;
}

function Dp_getday ()
{
var dp=this;
return Dp.curDate.getDate ();
}

function Dp_format (Sformat)
{
var dp=this;
Return Dp.curDate.format (Sformat);
}

function Dp_setaccesskey (SKey)
{
var dp=this;
var src=dp.children[0];
if (src && src.tagname== "INPUT")
{
Src.accesskey=skey;
}
}

function dp_getenabled ()
{
var dp=this;
var Val=false;

if (dp.dpenabled)
Val=true;
Else
Val=false;
return Val;
}

Function dp_setenabled (val)
{
 var dp=this
 var hr=false;
 
 var src= DP.CHILDREN[0];
 if (src && src.tagname== "INPUT")
 {
  src.disabled=!val;
   SRC=DP.CHILDREN[2];
  if (src && src.tagname== "INPUT")
  {
   src.disabled=!val;
   src=dp.children[4];
   if (src && src.tagname== "INPUT")
   {
     Src.disabled=!val;
    dp.dpEnabled=val;
    hr=true;
   }
  }
 }
 return HR;
}

function Dp_setnoborder ()
{
var dp=this;
Dp.style.border=0
Dp.children[0].style.color= "#ffffff"
Dp.children[1].style.color= "#ffffff"
Dp.children[2].style.color= "#ffffff"
Dp.children[3].style.color= "#ffffff"
Dp.children[4].style.color= "#ffffff"
Dp.children[5].style.color= "#ffffff"
Dp.children[6].style.color= "#ffffff"
Dp.children[7].style.color= "#ffffff"
}

function Dp_setfocus ()
{
var dp=this;
var src=dp.children[0];
if (src && src.tagname== "INPUT" &&!src.disabled)
{
Src.focus ();
}
}

Function dp_getdatecontent ()
{
 var dp=this
 var con= "";
 var syeardes= "";
 var smonthdes= "";
 var sdaydes= "";
 var src=dp.children[1];
 
 if (src && src.tagname== "FONT")
 {
  syeardes=src.innertext;
  src=dp.children[3];
  if (src && src.tagname== "FONT")
  {
   smonthdes= Src.innertext;
   src=dp.children[5];
   if (src && src.tagname== "FONT")
   {
     Sdaydes=src.innertext;
    var dt=dp.curdate;
    con=dt.getfullyear () +syeardes+ (Dt.getmonth () +1) +smonthdes+dt.getdate () +sDayDes;
   }
  }
 }
 return con;
}

function Dp_setformat (Sformat)
{
This.formatstring=sformat;
This.refreshposttext ();
}

function Dp_refreshposttext ()
{
var dp=this;
var sformat= "Yyyy/mm/dd";

if (typeof (dp.formatstring) = = "string")
sformat=dp.formatstring;
var txt=dp.children[8];
if (txt && txt.tagname== "INPUT")
Txt.value=dp.format (Sformat);
}

function Dp_initdropdown ()
{
var dp=this;
var ddt=dp.getdropdowntable ();
if (DDT)
{
Ddt.curcell=null;
var cell=null;
var dt=new Date (Dp.curDate.getFullYear (), Dp.curDate.getMonth (), 1);
CELL=DDT.ROWS[0].CELLS[1];
if (cell)
{
Cell.innertext=dp.getmonthname (Dt.getmonth ()) + "" +dt.getfullyear ();
}

var wd=dt.getday ();


Dt=new Date (Dt.getfullyear (), Dt.getmonth (), 1-WD);


var day=dt.getdate ();








for (var i=2; i&lt;8; i++)


{


for (var j=0; j&lt;7; J + +)


{


CELL=DDT.ROWS[I].CELLS[J];


if (cell)


{


if (Dp.curDate.getMonth ()!=dt.getmonth ())


Cell.classname= "Dpcellother";


else if (Dp.curDate.getDate ()!=dt.getdate ())


Cell.classname= "Dpcell";


Else


{


Cell.classname= "Dpcell";


Dp_oncell (cell);


}


Cell.innertext=day;


Cell.year=dt.getfullyear ();


Cell.month=dt.getmonth ();


Dt.setdate (day+1);


Day=dt.getdate ();


}


}


}


}


}

function Dp_getmonthname (lmonth)
{
var mnarr=new Array ("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
return Mnarr[lmonth];
}

function Dp_setweekname ()
{
var dp=this;
var ddt=dp.getdropdowntable ();
if (DDT)
{
var cell=null;

for (var j=0; j<7; J + +)
{
CELL=DDT.ROWS[1].CELLS[J];
if (cell)
{
CELL.INNERTEXT=ARGUMENTS[J];
}
}
}
}

Function Dp_showdropdown ()
{
 var dp=this
 var ddt=dp.getdropdowntable ();
 if (DDT)
 {
  if (ddt.style.display== "block")
  {
    ddt.style.zindex=10;
   dp.hidedropdown ();
  }
  else
  {
   dp.initdropdown ();
   if ( ddt.clientwidth==0)
   {
    ddt.style.pixelleft=-500;
     ddt.style.pixelTop=-500;
    ddt.style.display= "Block";
   }

var ddtwidth=ddt.clientwidth==0?266:ddt.clientwidth;
var ddtheight=ddt.clientheight==0?133:ddt.clientheight;

var lleft=getoffsetleft (DP);
var ltop=getoffsettop (DP) +dp.offsetheight;

if ((ltop+ddtheight) > (document.body.clientheight+document.body.scrolltop)
{
ltop-= (ddtheight+dp.offsetheight+2);
}

if ((Lleft+ddtwidth) > (document.body.clientwidth+document.body.scrollleft)
{
Lleft=document.body.clientwidth+document.body.scrollleft-ddtwidth-2;
}

var off=parseint (dp.offsethor,10);
if (isNaN (off))
off=0;
Ddt.style.pixelleft=lleft+off;
Ddt.style.pixeltop=ltop;
ddt.style.zindex=500;
Ddt.dpolddocclick=document.onclick;
Ddt.dpolddockeydown=document.onkeydown;

Event.cancelbubble=true;
Event.returnvalue=false;
Document.onclick=dp_sub_docclick;
Document.onkeydown=dp_sub_dockeydown;

ddt.style.display= "Block";
}
}
}

function getdropdowntable ()
{
var ddt=document.all ("Dpdropdowntable");
if (!) ( DDT && ddt.tagname== "TABLE"))
Ddt=null;
return to DDT;
}

function Dp_hidedropdown ()
{
var ddt=getdropdowntable ();
if (DDT)
{
Ddt.style.display= "None";
Document.onclick=ddt.dpolddocclick;
Document.onkeydown=ddt.dpolddockeydown;
}
}

function dp_getdropdowntable ()
{
var dp=this;
Dp.dropdowntable=dp_createdropdown ();

if (dp.dropdowntable && dp.dropdowntable.tagname== "TABLE")
{
DP.DROPDOWNTABLE.DP=DP;
return dp.dropdowntable;
}
Else
return null;
}

Function Dp_ondatechange ()
{
 var dp=this
 if (Dp.curDate.compare (dp.olddate)!=0)
 {
  dp.olddate=dp.curdate.clone ();
  dp.refreshview ();
   Dp.refreshposttext ();
  if (typeof (dp.datechanged) = = "function")
   dp.datechanged ( Dp.curDate.getFullYear (), Dp.curDate.getMonth () +1,dp.curdate.getdate ());
 }
}

function Dp_refreshview ()
{
var dp=this;
var Hr=false;

if (DP && dp.curdate)
{
var src=dp.children[0];
if (src && src.tagname== "INPUT")
{
Src.value=dp.curdate.getfullyear ();
SRC=DP.CHILDREN[2];
if (src && src.tagname== "INPUT")
{
Src.value=dp.curdate.getmonth () +1;
SRC=DP.CHILDREN[4];
if (src && src.tagname== "INPUT")
{
Src.value=dp.curdate.getdate ();
Hr=true;
}
}
}
}
return HR;
}

function Dp_settabindex (ltabindex)


{


var dp=this;


var Hr=false;





if (DP)


{


var src=dp.children[0];


if (src &amp;&amp; src.tagname== "INPUT")


{


Src.tabindex=ltabindex;


SRC=DP.CHILDREN[2];


if (src &amp;&amp; src.tagname== "INPUT")


{


Src.tabindex=ltabindex;


SRC=DP.CHILDREN[4];


if (src &amp;&amp; src.tagname== "INPUT")


{


Src.tabindex=ltabindex;


SRC=DP.CHILDREN[7];


if (src &amp;&amp; src.tagname== "A")


{


Src.tabindex=ltabindex;


Hr=true;


}


}


}


}


}


return HR;


}

function Dp_setdatedes (syeardes,smonthdes,sdaydes)
{
if (syeardes==null)
Syeardes= "-";
if (smonthdes==null)
Smonthdes= "-";
if (sdaydes==null)
Sdaydes= "";

var dp=this;
var Hr=false;

var src=dp.children[1];
if (src && src.tagname== "FONT")
{
Src.innertext=syeardes;
SRC=DP.CHILDREN[3];
if (src && src.tagname== "FONT")
{
Src.innertext=smonthdes;
SRC=DP.CHILDREN[5];
if (src && src.tagname== "FONT")
{
Src.innertext=sdaydes;
Hr=true;
}
}
}
return HR;
}

function Dp_setmaxdate (lyear,lmonth,lday)


{


var dp=this;


var Hr=false;





if (DP)


{


Lyear=parseint (lyear,10);


Lmonth=parseint (lmonth,10);


Lday=parseint (lday,10);





if (!) ( isNaN (lyear) | | isNaN (lmonth) | | isNaN (Lday))


{


lmonth--;


var dt=new Date (Lyear,lmonth,lday);


var dmin=new Date (Dp.minyear,dp.minmonth,dp.minday);


var cdmax=new Date (Cl_dpmaxyear,cl_dpmaxmonth,cl_dpmaxday);





if (Dt.compare (Cdmax) &lt;=0 &amp;&amp; dt.compare (dMin) &gt;=0)


{


Dp.maxyear=dt.getfullyear ();


Dp.maxmonth=dt.getmonth ();


Dp.maxday=dt.getdate ();


Hr=true;


}


}


}


return HR;


}

function Dp_setmindate (lyear,lmonth,lday)


{


var dp=this;


var Hr=false;





if (DP)


{


Lyear=parseint (lyear,10);


Lmonth=parseint (lmonth,10);


Lday=parseint (lday,10);





if (!) ( isNaN (lyear) | | isNaN (lmonth) | | isNaN (Lday))


{


lmonth--;


var dt=new Date (Lyear,lmonth,lday);


var dmax=new Date (Dp.maxyear,dp.maxmonth,dp.maxday);


var cdmin=new Date (Cl_dpminyear,cl_dpminmonth,cl_dpminday);





if (Dt.compare (dMax) &lt;=0 &amp;&amp; dt.compare (cdmin) &gt;=0)


{


Dp.minyear=dt.getfullyear ();


Dp.minmonth=dt.getmonth ();


Dp.minday=dt.getdate ();


Hr=true;


}


}


}


return HR;


}

function Dp_setcurdate (lyear,lmonth,lday)
{
var dp=this;
var Hr=false;

Lyear=parseint (lyear,10);
Lmonth=parseint (lmonth,10);
Lday=parseint (lday,10);

if (!) ( isNaN (lyear) | | isNaN (lmonth) | | isNaN (Lday))
{
var dt=new Date (Lyear,lmonth-1,lday);
var dmax=new Date (Dp.maxyear,dp.maxmonth,dp.maxday);
var dmin=new Date (Dp.minyear,dp.minmonth,dp.minday);
if (Dt.compare (dMax) <=0 && dt.compare (dMin) >=0)
{
DP.CURDATE=DT;
Dp.ondatechange ();
Hr=true;
}
}

if (!HR)
Dp.refreshview ();
return HR;
}

function Dp_dropclick ()
{
var src=event.srcelement;
var dp=getparentfromsrc (src, "SPAN")
if (DP && dp.classname== "dpframe" && dp.dpenabled)
{
Dp.showdropdown ();
}
}

function Dp_focus (srctype)
{
var src=event.srcelement;
if (src && src.tagname== "INPUT")
{
Switch (srctype)
{
Case ' Year ':
Break
Case ' Month ':
Break
Case ' Day ':
Break
Default:;
}
Src.select ();
}
return true;
}

function Dp_blur (srctype)


{


var src=event.srcelement;


var dp=getparentfromsrc (src, "SPAN")


if (src &amp;&amp; src.tagname== "INPUT" &amp;&amp; dp &amp;&amp; dp.classname== "Dpframe")


{


var lyear=dp.curdate.getfullyear ();


var lmonth=dp.curdate.getmonth () +1;


var lday=dp.curdate.getdate ();





var val=parseint (src.value,10);


if (isNaN (val))


Val=-1;


Switch (srctype)


{


Case ' Year ':


Lyear=val==-1?lyear:val;


Break


Case ' Month ':


Lmonth=val==-1?lmonth:val;


Break


Case ' Day ':


Lday=val==-1?lday:val;


Break


Default:;


}


Dp.setcurdate (Lyear,lmonth,lday);


if (val==-1)


Dp.refreshview ();


}


return true;


}

Function Dp_keydown (srctype)
{
 var src=event.srcelement;
 var dp=getparentfromsrc (SRC, " SPAN ")
 var brefresh=true
 
 if (dp && dp.classname==" Dpframe ")
 {
  var lyear=dp.curdate.getfullyear ();
  var lmonth=dp.curdate.getmonth ();
  var lday=dp.curdate.getdate ();
  var lstep=0;
  
  switch (event.keycode)
  {
   case:
     lStep=1;
    break;
   case:
    lstep=-1
    break
    case:
    event.keycode=9;
    break;
    default:
    brefresh=false;
  }

  switch (srctype)
  {
   case ' year ':
     Lyear+=lstep;
    break;
   case ' month ':
    lmonth+=lstep;
    break;
   case ' Day ':
    lday+=lstep;
    break;
   default:;
  }
  if (Brefresh)
   dp.setcurdate (lyear,lmonth+1,lday);
 }
  return true;
}

Function Dp_monthchange (lstep)
{
 var src=event.srcelement;
 if (src)
 {
 & Nbsp;var ddt=getdropdowntable ();
  if (DDT && ddt.dp)
  {
   var dt=ddt.dp.curdate.clone ();
   var Loldmonth=dt.getmonth ();
   var loldday=dt.getdate ();
    
   dt.setdate (1);
   dt.setmonth (lOldMonth+ LSTEP+1);
   dt.setdate (0);
   if (Dt.getdate () >loldday)
    dt.setdate (LOldDay);
    if (Ddt.dp.setCurDate (Dt.getfullyear (), Dt.getmonth () +1,dt.getdate ())
     Ddt.dp.initDropDown ();
  }
 }
}

Function Dp_ddt_click ()
{
 var src=event.srcelement
 if (src && src.tagname== "TD")
 {
  var ddt=getdropdowntable ()
  if (DDT && ddt.dp)
  {
   var loldmonth=ddt.dp.curdate.getmonth ();
   if (Ddt.dp.setCurDate (Src.year,parseint (src.month,10) +1,parseint (src.innertext,10))
   {
    if (src.month!=loldmonth)
      Ddt.dp.initDropDown ();
    else
     dp_oncell (src);
   }
&NBSP;&NBSP}
 }

Function Dp_oncell (src)
{
 var row=src.parentelement
 if (Row && row.tagname== "TR" && row.rowindex>1)
 {
  var ddt=getdropdowntable ();
  if (DDT)
  {
   if (Ddt.curcell)
    ddt.curcell.classname= Ddt.curcelloldclass;
   ddt.curCellOldClass=src.className;
   src.classname= "Dpcellselect";
   ddt.curCell=src;
  }
 }
}

function Dp_ddt_dblclick ()
{
var src=event.srcelement;
if (src && src.tagname== "TD")
{
var ddt=getdropdowntable ();
if (DDT && DDT.DP)
{
var loldmonth=ddt.dp.curdate.getmonth ();
if (Ddt.dp.setCurDate (Src.year,parseint (src.month,10) +1,parseint (src.innertext,10))
{
Ddt.dp.hideDropDown ();
}
}
}
}

function Dp_sub_docclick ()
{
var src=event.srcelement;
var ddt=getparentfromsrc (src, "TABLE");
if (!DDT | | ddt.id!= "dpdropdowntable")
{
Dp_hidedropdown ();
}
Event.cancelbubble=true;
Event.returnvalue=false;

return false;
}

function Dp_sub_dockeydown ()
{
Dp_hidedropdown ();
return true;
}

function Initdateobject ()
{
Date.prototype.compare=date_compare;
Date.prototype.clone=date_clone;
Date.prototype.format=date_format;
}

function Date_format (Sformat)
{
var dt=this;
if (Sformat==null | | typeof (Sformat)!= "string")
Sformat= "";
Sformat=sformat.replace (/yyyy/ig,dt.getfullyear ());
var y= "" +dt.getyear ();
if (y.length>2)
{
Y=y.substring (y.length-2,y.length);
}
Sformat=sformat.replace (/yy/ig,y);
Sformat=sformat.replace (/mm/ig,dt.getmonth () +1);
Sformat=sformat.replace (/dd/ig,dt.getdate ());
return sformat;
}

function Date_clone ()
{
return new Date (This.getfullyear (), This.getmonth (), this.getdate ());
}

function Date_compare (dtcompare)
{
var dt=this;
var hr=0;

if (dt && dtcompare)
{
if (Dt.getfullyear () >dtcompare.getfullyear ())
Hr=1;
else if (Dt.getfullyear () <dtcompare.getfullyear ())
Hr=-1;
else if (Dt.getmonth () >dtcompare.getmonth ())
Hr=1;
else if (Dt.getmonth () <dtcompare.getmonth ())
Hr=-1;
else if (Dt.getdate () >dtcompare.getdate ())
Hr=1;
else if (Dt.getdate () <dtcompare.getdate ())
Hr=-1;
}
return HR;
}

function date_getdatefromvt_date (DT)
{
Dt=dt.replace (/-/g, "/");
Dt=date.parse (DT);
if (isNaN (DT))
Dt=null;
Else
Dt=new Date (DT);
return DT;
}

Call the Initialize function
Initdateobject ();

function Keyfilter (type)


{


var Berr=false;





Switch (type)


{


Case ' Date ':


if (!) ( Event.keycode = 45 | | Event.keycode = 47 | | (event.keycode&gt;=48 &amp;&amp; event.keycode&lt;=57))


Berr=true;


Break


Case ' number ':


if (!) ( event.keycode&gt;=48 &amp;&amp; event.keycode&lt;=57))


Berr=true;


Break


Case ' CY ':


if (!) ( Event.keycode = 46 | | (event.keycode&gt;=48 &amp;&amp; event.keycode&lt;=57))


Berr=true;


Break


Case ' long ':


if (!) ( Event.keycode = 45 | | (event.keycode&gt;=48 &amp;&amp; event.keycode&lt;=57))


Berr=true;


Break


Case ' Double ':


if (!) ( Event.keycode = 45 | | Event.keycode = 46 | | (event.keycode&gt;=48 &amp;&amp; event.keycode&lt;=57))


Berr=true;


Break


Default


if (Event.keycode = | | | event.keycode = = | | event.keycode==38)


Berr=true;


}


return!berr;


}

function Getparentfromsrc (Src,partag)
{
if (src && src.tagname!=partag)
SRC=GETPARENTFROMSRC (Src.parentelement,partag);
return SRC;
}

Function switchtooption (sel,newoption,bywhat)
{
 newoption=newoption.tostring ();
 if ( Newoption && sel && sel.tagname== "SELECT")
 {
  newoption=trim (newoption);
  var opts=sel.options;
  for (var i=0;i<opts.length;i++)
  {
   if (Trim (opts[i][bywhat) . toString ()) ==newoption)
   {
    sel.selectedindex=i;
     break;
   }
  }
 }
}

is a element visible?
function iselementvisible (SRC)
{
if (SRC)
{
var x=getoffsetleft (SRC) +2-document.body.scrollleft;
var y=getoffsettop (SRC) +2-document.body.scrolltop;
if (Ptisinrect (x,y,0,0,document.body.offsetwidth,document.body.offsetheight))
{
var e=document.elementfrompoint (x,y);
return src==e;
}
}

return false;
}

function Ptisinrect (x,y,left,top,right,bottom)
{
Return (X>=left && x<right) && (y>=top && y<bottom);
}

Function Getoffsetleft (src) {
 var set=0
 if (src)
 {
  if ( src.offsetparent)
   set+=src.offsetleft+getoffsetleft (src.offsetparent);
  
  if (src.tagname!= "body")
  {
   var x=parseint (src.scrollleft,10) ;
   if (!isnan (x))
    set-=x
  
 
  return set;
}
Function Getoffsettop (src) {
 var set=0
 if (src)
 {
  if ( src.offsetparent)
   set+=src.offsettop+getoffsettop (src.offsetparent);
  
  if (src.tagname!= "body")
  {
   var y=parseint (src.scrolltop,10);
   if (!isnan (y))
    set-=y;
  }
 
 return Set
}

Function isanylevelparent (src,par)
{
 var hr=false;
 if (Src==par)
  hr=true;
 else if (src!=null)
  hr=isanylevelparent (src.parentelement,par);
 
 return HR ;
}

Function Isie (version)
{
 var i0=navigator.appversion.indexof ("MSIE")
 var i1=-1;
  var ver=0;
 if (i0>=0)
 {
  i1=navigator.appversion.indexof ("", i0+1);
  if (i1>=0)
  {
   i0=i1;
   i1= Navigator.appVersion.indexOf (";", i0+1);
   if (i1>=0)
   {
    ver=parsefloat ( Navigator.appVersion.substring (I0+1,I1));
    if (isNaN (ver))
     ver=0;
   }
&NBSP;&NBSP}
 }
 
 return (navigator.userAgent.indexOf ("MSIE")!=-1
   && navigator.userAgent.indexOf ("Windows")!=-1
  && (ver< (version+1) && Ver>=version) | | version==0));
}

Function getvaliddate (str)
{
 var sdate=str.replace (///g, "-");
 var varr=sdate.split ("-");
 var sret= "";
 
 if (varr.length>=3)
 {
  var year=parseint (varr[0],10);
   var Month=parseint (varr[1],10);
  var Day=parseint (varr[2],10);
  if (!) ( isNaN (year) | | isNaN (month) | | isNaN (day))
   if (year>=1900 && year<9999 && month>=1 && Month <=12)
   {
    var dt=new Date (year,month-1,day);
     year=dt.getfullyear ();
    month=dt.getmonth () +1;
    day=dt.getdate ();
    sret=year+ "-" + (month<10?) 0 ":" "+month+"-"+ (day<10?") 0 ":") +day;
   }
 }
 
 return sret;
}

Function Getsafevalue (val,def)
{
 if (typeof (val) = = ' undefined ' | | val==null)
  return def
 else
  return val;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.