A code that is useful in time selection

Source: Internet
Author: User
Tags date array current time date1 getdate idate tagname visibility

Recently, the company is doing a system, to use a time to select the control, is originally using ASP. NET to do, but in the end I will use JS to write

Specific code (part):
Time. Js
--------------------------------------

Please ensure the integrity of the version
function Ftime (ihour,isec,imin)
{
return ihour+isec+imin;
}
Current date
<input type= "text" size= "name=" Datbegin "value=" <%=formatdatetime (dateandtime_start,2)%> "Maxlength=" "Ten" ReadOnly > Day <%h_time (int (hour (dateandtime_start)), Dathourbegin (%>) <%h_time (int ( Dateandtime_start)), "Datminutebegin"%> minute (date format: YYYY-MM-DD)
function Getnowdate ()
{
var nn=new Date ();
Year1=nn.getyear ();
Mon1=nn.getmonth () +1;
Date1=nn.getdate ();
var monstr1;
var datestr1
if (mon1<10)
monstr1= "0" +mon1;
Else
Monstr1= "" +mon1;

if (date1<10)
datestr1= "0" +DATE1;
Else
Datestr1= "" +DATE1;
return year1+ "-" +monstr1+ "-" +DATESTR1;
}
Target date
function Getlastweekdate ()
{
var nn=new Date ();
Year1=nn.getyear ();
Mon1=nn.getmonth () +1;
Date1=nn.getdate ();

var mm=new Date (year1,mon1-1,date1);
var tmp1=new Date (2000,1,1);
var tmp2=new Date (2000,1,15);
var ne=tmp2-tmp1;
var mm2=new Date ();
Mm2.settime (Mm.gettime ()-ne);




Year2=mm2.getyear ();
Mon2=mm2.getmonth () +1;
Date2=mm2.getdate ();


if (mon2<10)
monstr2= "0" +mon2;
Else
Monstr2= "" +mon2;

if (date2<10)
datestr2= "0" +DATE2;
Else
Datestr2= "" +DATE2;


return year2+ "-" +monstr2+ "-" +DATESTR2;
}

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 ();
function Fsettoday ()//direct access to current time
{

}

function Fsetdate (iyear, Imonth, Iday) {

VicPopCal.style.visibility = "hidden";
if ((iyear = 0) && (imonth = = 0) && (iday = 0)) {
Gdctrl.value = "";
}else{
Imonth = imonth + 100 + "";
Imonth = imonth.substring (1);
Iday = iday + 100 + "";
Iday = iday.substring (1);
var t = "Software only does test use";

if (Gdctrl.tagname = = "INPUT") {
Gdctrl.value = iyear+ "-" +imonth+ "-" +iday+ "" + t;
}else{
Gdctrl.innertext = iyear+ "-" +imonth+ "-" +iday+ "" +t;
}
}

For (i in Goselecttag)
goselecttag[i].style.visibility = "visible";
goselecttag.length = 0;

Window.returnvalue=gdctrl.value;
Window.close ();

}

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);
}

function point (IX, IY) {
This.x = IX;
This.y = IY;
}

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 ("Day", "one", "two", "three", "four", "five", "six");
var styletd = "bgcolor=" "+gcbg+" ' bordercolor= ' "+gcbg+" ' valign= ' Middle ' ' align= ' center ' height= ' "+icellheight+" ' style            = ' font:bold Arial ' +sdatetextsize+ '; Coded by homocysteine Email:hcy110@263.net

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; ' >");    write ("<font id=celltext victor= ' hcy_flag ' > </font>");    
   write ("</td>")
  }
  write ("</tr>");
&NBSP}
 }
}

function fupdatecal (iyear, Imonth) {
Mymonth = fbuildcal (iyear, imonth);
var i = 0;
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];

}
}
}

function Fsetyearmon (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 Fprevmonth () {
var imon = Tbselmonth.value;
var iyear = Tbselyear.value;

if (--imon<1) {
Imon = 12;
iyear--;
}

Fsetyearmon (Iyear, Imon);
}

function Fnextmonth () {
var imon = Tbselmonth.value;
var iyear = Tbselyear.value;

if (++imon>12) {
Imon = 1;
iyear++;
}

Fsetyearmon (Iyear, Imon);
}

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;
}

Main:popctrl is the widget beyond which your want this calendar to appear;
Datectrl is the widget into which and you want to put the selected date.
i.e.: <input type= "text" name= "DC" style= "Text-align:center" readonly><input type= "button" value= "V" >
function Fpopcalendar (Popctrl, datectrl,strdate) {
if (Popctrl = = Previousobject) {
if (VicPopCal.style.visibility = = "visible") {
Hiddendiv ();
return true;
}

}
Previousobject = Popctrl;
Gdctrl = Datectrl;
Finitialdate (strdate);
Fsetyearmon (Giyear, gimonth);
var point = Fgetxy (Popctrl);
With (Vicpopcal.style) {
left = point.x+10; The coordinates of the pop-up window
top = point.y+popctrl.offsetheight+15;
width = vicpopcal.offsetwidth;
width = 180; //
Height = vicpopcal.offsetheight;
Height = 180;
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 (Nyear < 1900 | | | nyear > 2100) return false;
if (Nmonth < 1 | | | nmonth >) return false;
if (Nday < 1 | | | nday > ARRMON[NMONTH-1]) return false;

Giyear = Nyear;
Gimonth = Nmonth;
Giday = Nday;
return true;
}

var gmonths = new Array ("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");

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 ' > ');
Write ("<select name= ' tbselyear ' >");
for (i=1950;i<2030;i++)
Write ("<option value= '" +i+ "' >" +i+ "Year </OPTION>");
Write ("</SELECT>");
Write ("<select name= ' Tbselmonth ' >");
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:20 ' >");
Write ("</td>");
Write ("</TR><TR>");
Write ("<td align= ' center ' >");
Write ("<div style= ' background-color:teal ' ><table width= ' 100% ' border= ' 0 ' >");
Fdrawcal (giyear, gimonth, 0, ' 0 ');
Write ("</table></DIV>");
Write ("</td>");
Write ("</tr><tr><td align= ' center ' >");


//
Write ("<select name= ' Tbselhour ' >");
for (i=0;i<24;i++)
{
if (i<10)
{
Write ("<option value= ' 0" +i+ "' >" +i+ "</OPTION>");
}
Else
{
Write ("<option value= '" +i+ "' >" +i+ "</OPTION>");
}
}
Write ("</SELECT>");

Write ("<select name= ' tbselsec ' >");
for (i=0;i<60;i++)
if (i<10)
{
Write ("<option value= ' 0" +i+ "' >" +i+ "cent </OPTION>");
}
Else
{
Write ("<option value= '" +i+ "' >" +i+ "cent </OPTION>");
}
Write ("</SELECT>");

Write ("<select name= ' tbselmin ' >");
for (i=0;i<60;i++)
if (i<10)
{
Write ("<option value= ' 0" +i+ "' >" +i+ "sec </OPTION>");
}
Else
{
Write ("<option value= '" +i+ "' >" +i+ "sec </OPTION>");
}
Write ("</SELECT>");
//


Write ("<table width= ' 100% ' ><tr><td align= ' center ' >");
Write ("<font size= ' 2 ' > Empty </font>");
B style= ' Cursor:hand ' is replaced with font size= ' 2 '
Write ("</td><td algin= ' center ' >");
Write ("<font size= ' 2" > Today: "+giyear+"-"+gimonth+"-"+giday+" </font> ");
B style= ' Cursor:hand ' is replaced with font size= ' 2 '
Write ("</td></tr></table>");
Write ("</TD></TR>");
Write ("</TABLE></Div>");
}


Html:

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en"
"http://www.w3.org/TR/html4/loose.dtd" >
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<title> Untitled Document </title>
<style type= "Text/css" >
<!--
. style1 {font-size:10px}
-->
</style>

<body>

<body bgcolor= "#D6D3CE" >

<script language= "javascript" src= "Time.js" >//Calling JS code
</script>
<div align= "center" >
<center>
<table width= "248" border= "0" >
<tr>
<TD nowrap width= "599" ><span class= "Style1" > select time </span>:<input class= "input" type= "text" Name= " RegDate "Size=" ></td>
</tr>
</table>
</center>
</div>

</body>

</body>



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.