JavaScript時(shí)間日期控件
當(dāng)前位置:點(diǎn)晴教程→知識(shí)管理交流
→『 技術(shù)文檔交流 』
[p]calendar.js(注:在.net環(huán)境下.js文件需存為utf-8格式)[/p]
[p][br]/**//**[br]*本日歷選擇控件由tiannet根據(jù)前人經(jīng)驗(yàn)完善而得。大部分代碼來自meizz的日歷控件。[br]*tiannet添加了時(shí)間選擇功能、select,object標(biāo)簽隱藏功能,還有其它小功能。[br]*使用方法:[br]* (1)只選擇日期 [br]* (2)選擇日期和小時(shí) [br]* (3)選擇日期和小時(shí)及分鐘 [br]*設(shè)置參數(shù)的方法[br]* (1)設(shè)置日期分隔符 setdatesplit(strsplit);默認(rèn)為"-"[br]* (2)設(shè)置日期與時(shí)間之間的分隔符 setdatetimesplit(strsplit);默認(rèn)為" "[br]* (3)設(shè)置時(shí)間分隔符 settimesplit(strsplit);默認(rèn)為":"[br]* (4)設(shè)置(1),(2),(3)中的分隔符 setsplit(strdatesplit,strdatetimesplit,strtimesplit);[br]* (5)設(shè)置開始和結(jié)束年份 setyearperiod(intdatebeg,intdateend)[br]*說明:[br]* 默認(rèn)返回的日期時(shí)間格式如同:2005-02-02 08:08[br]*/[br]//------------------ 樣式定義 ---------------------------//[br]//功能按鈕同樣樣式[br]var s_tiannet_turn_base = "height:16px;color:white;border:0 solid #cccccc;cursor:hand;background-color:#2650a6;";[br]//翻年、月等的按鈕[br]var s_tiannet_turn = "width:28px;" + s_tiannet_turn_base;[br]//關(guān)閉、清空等按鈕樣式[br]var s_tiannet_turn2 = "width:22px;" + s_tiannet_turn_base;[br]//年選擇下拉框[br]var s_tiannet_select = "width:64px;display:none;";[br]//月、時(shí)、分選擇下拉框[br]var s_tiannet_select2 = "width:46px;display:none;";[br]//日期選擇控件體的樣式[br]var s_tiannet_body = "width:150;background-color:#2650a6;display:none;z-index:9998;position:absolute;" +[br]"border-left:1 solid #cccccc;border-top:1 solid #cccccc;border-right:1 solid #999999;border-bottom:1 solid #999999;";[br]//顯示日的td的樣式[br]var s_tiannet_day = "width:21px;height:20px;background-color:#d8f0fc;";[br]//字體樣式[br]var s_tiannet_font = "color:#ffcc00;cursor:hand;";[br]//鏈接的樣式[br]var s_tiannet_link = "text-decoration:none;color:#2650a6;";[br]//橫線[br]var s_tiannet_line = "border-bottom:1 solid #6699cc";[br]//------------------ 變量定義 ---------------------------//[br]var tiannetyearst = 1950;//可選擇的開始年份[br]var tiannetyearend = 2010;//可選擇的結(jié)束年份[br]var tiannetdatenow = new date();[br]var tiannetyear = tiannetdatenow.getfullyear(); //定義年的變量的初始值[br]var tiannetmonth = tiannetdatenow.getmonth()+1; //定義月的變量的初始值[br]var tiannetday = tiannetdatenow.getdate();[br]var tiannethour = 8;//tiannetdatenow.gethours();[br]var tiannetminute = 0;//tiannetdatenow.getminutes();[br]var tiannetarrday=new array(42); //定義寫日期的數(shù)組[br]var tiannetdatesplit = "-"; //日期的分隔符號(hào)[br]var tiannetdatetimesplit = " "; //日期與時(shí)間之間的分隔符[br]var tiannettimesplit = ":"; //時(shí)間的分隔符號(hào)[br]var tiannetoutobject; //接收日期時(shí)間的對(duì)象[br]var arrtiannethide = new array();//被強(qiáng)制隱藏的標(biāo)簽[br]var m_bolshowhour = false;//是否顯示小時(shí)[br]var m_bolshowminute = false;//是否顯示分鐘[/p]
[p]var m_amonhead = new array(12); //定義陽歷中每個(gè)月的最大天數(shù)[br] m_amonhead[0] = 31; m_amonhead[1] = 28; m_amonhead[2] = 31; m_amonhead[3] = 30; m_amonhead[4] = 31; m_amonhead[5] = 30;[br] m_amonhead[6] = 31; m_amonhead[7] = 31; m_amonhead[8] = 30; m_amonhead[9] = 31; m_amonhead[10] = 30; m_amonhead[11] = 31;[br]// ---------------------- 用戶可調(diào)用的函數(shù) -----------------------------//[br]//用戶主調(diào)函數(shù)-只選擇日期[br]function setday(obj){[br]tiannetoutobject = obj;[br]//如果標(biāo)簽中有值,則將日期初始化為當(dāng)前值[br]var strvalue = tiannettrim(tiannetoutobject.value);[br]if( strvalue != "" ){[br]tiannetinitdate(strvalue);[br]}[br]tiannetpopcalendar();[br]}[br]//用戶主調(diào)函數(shù)-選擇日期和小時(shí)[br]function setdayh(obj){[br]tiannetoutobject = obj;[br]m_bolshowhour = true;[br]//如果標(biāo)簽中有值,則將日期和小時(shí)初始化為當(dāng)前值[br]var strvalue = tiannettrim(tiannetoutobject.value);[br]if( strvalue != "" ){[br]tiannetinitdate(strvalue.substring(0,10));[br]var hour = strvalue.substring(11,13);[br]if( hour < 10 ) tiannethour = hour.substring(1,2);[br]}[br]tiannetpopcalendar();[br]}[br]//用戶主調(diào)函數(shù)-選擇日期和小時(shí)及分鐘[br]function setdayhm(obj){[br]tiannetoutobject = obj;[br]m_bolshowhour = true;[br]m_bolshowminute = true;[br]//如果標(biāo)簽中有值,則將日期和小時(shí)及分鐘初始化為當(dāng)前值[br]var strvalue = tiannettrim(tiannetoutobject.value);[br]if( strvalue != "" ){[br]tiannetinitdate(strvalue.substring(0,10));[br]var time = strvalue.substring(11,16);[br]var arr = time.split(tiannettimesplit);[br]tiannethour = arr[0];[br]tiannetminute = arr[1];[br]if( tiannethour < 10 ) tiannethour = tiannethour.substring(1,2);[br]if( tiannetminute < 10 ) tiannetminute = tiannetminute.substring(1,2);[br]}[br]tiannetpopcalendar();[br]}[br]//設(shè)置開始日期和結(jié)束日期[br]function setyearperiod(intdatebeg,intdateend){[br]tiannetyearst = intdatebeg;[br]tiannetyearend = intdateend;[br]}[br]//設(shè)置日期分隔符。默認(rèn)為"-"[br]function setdatesplit(strdatesplit){[br]tiannetdatesplit = strdatesplit;[br]}[br]//設(shè)置日期與時(shí)間之間的分隔符。默認(rèn)為" "[br]function setdatetimesplit(strdatetimesplit){[br]tiannetdatetimesplit = strdatetimesplit;[br]}[br]//設(shè)置時(shí)間分隔符。默認(rèn)為":"[br]function settimesplit(strtimesplit){[br]tiannettimesplit = strtimesplit;[br]}[br]//設(shè)置分隔符[br]function setsplit(strdatesplit,strdatetimesplit,strtimesplit){[br]tiannetdatesplit(strdatesplit);[br]tiannetdatetimesplit(strdatetimesplit);[br]tiannettimesplit(strtimesplit);[br]}[br]//設(shè)置默認(rèn)的日期。格式為:yyyy-mm-dd[br]function setdefaultdate(strdate){[br]tiannetyear = strdate.substring(0,4);[br]tiannetmonth = strdate.substring(5,7);[br]tiannetday = strdate.substring(8,10);[br]}[br]//設(shè)置默認(rèn)的時(shí)間。格式為:hh24:mi[br]function setdefaulttime(strtime){[br]tiannethour = strtime.substring(0,2);[br]tiannetminute = strtime.substring(3,5);[br]}[br]// ---------------------- end 用戶可調(diào)用的函數(shù) -----------------------------//[br]//------------------ begin 頁面顯示部分 ---------------------------//[br]var weekname = new array("日","一","二","三","四","五","六");[br]document.write('
');[br]document.write(' ');[br]//------------------ end 頁面顯示部分 ---------------------------//[br]//------------------ 顯示日期時(shí)間的span標(biāo)簽響應(yīng)事件 ---------------------------//[br]//單擊年份span標(biāo)簽響應(yīng)[br]function spanyearcevent(){[br]hideelementsbyid(new array("seltianyear","tiannetmonthhead"),false);[br]if(m_bolshowhour) hideelementsbyid(new array("tiannethourhead"),false);[br]if(m_bolshowminute) hideelementsbyid(new array("tiannetminutehead"),false);[br]hideelementsbyid(new array("tiannetyearhead","seltianmonth","seltianhour","seltianminute"),true);[br]}[br]//單擊月份span標(biāo)簽響應(yīng)[br]function spanmonthcevent(){[br]hideelementsbyid(new array("seltianmonth","tiannetyearhead"),false);[br]if(m_bolshowhour) hideelementsbyid(new array("tiannethourhead"),false);[br]if(m_bolshowminute) hideelementsbyid(new array("tiannetminutehead"),false);[br]hideelementsbyid(new array("tiannetmonthhead","seltianyear","seltianhour","seltianminute"),true);[br]}[br]//單擊小時(shí)span標(biāo)簽響應(yīng)[br]function spanhourcevent(){[br]hideelementsbyid(new array("tiannetyearhead","tiannetmonthhead"),false);[br]if(m_bolshowhour) hideelementsbyid(new array("seltianhour"),false);[br]if(m_bolshowminute) hideelementsbyid(new array("tiannetminutehead"),false);[br]hideelementsbyid(new array("tiannethourhead","seltianyear","seltianmonth","seltianminute"),true);[br]}[br]//單擊分鐘span標(biāo)簽響應(yīng)[br]function spanminutecevent(){[br]hideelementsbyid(new array("tiannetyearhead","tiannetmonthhead"),false);[br]if(m_bolshowhour) hideelementsbyid(new array("tiannethourhead"),false);[br]if(m_bolshowminute) hideelementsbyid(new array("seltianminute"),false);[br]hideelementsbyid(new array("tiannetminutehead","seltianyear","seltianmonth","seltianhour"),true);[br]}[br]//根據(jù)標(biāo)簽id隱藏或顯示標(biāo)簽[br]function hideelementsbyid(arrid,bolhide){[br]var strdisplay = "";[br]if(bolhide) strdisplay = "none";[br]for(var i = 0;i < arrid.length;i ++){[br]var obj = document.getelementbyid(arrid[i]);[br]obj.style.display = strdisplay;[br]}[br]}[br]//------------------ end 顯示日期時(shí)間的span標(biāo)簽響應(yīng)事件 ---------------------------//[br]//判斷某年是否為閏年[br]function ispinyear(year){[br]var bolret = false;[br]if (0==year%4&&((year%100!=0)||(year%400==0))) {[br]bolret = true;[br]}[br]return bolret;[br]}[br]//得到一個(gè)月的天數(shù),閏年為29天[br]function getmonthcount(year,month){[br]var c=m_amonhead[month-1];[br]if((month==2)&&ispinyear(year)) c++;[br]return c;[br]}[br]//重新設(shè)置當(dāng)前的日。主要是防止在翻年、翻月時(shí),當(dāng)前日大于當(dāng)月的最大日[br]function setrealdaycount() {[br]if( tiannetday > getmonthcount(tiannetyear,tiannetmonth) ) {[br]//如果當(dāng)前的日大于當(dāng)月的最大日,則取當(dāng)月最大日[br]tiannetday = getmonthcount(tiannetyear,tiannetmonth);[br]}[br]}[br]//在個(gè)位數(shù)前加零[br]function addzero(value){[br]if(value < 10 ){[br]value = "0" + value;[br]}[br]return value;[br]}[br]//取出空格[br]function tiannettrim(str) {[br]return str.replace(/(^\s*)|(\s*$)/g,"");[br]}[br]//為select創(chuàng)建一個(gè)option[br]function createoption(objselect,value,text){[br]var option = document.createelement("option");[br]option.value = value;[br]option.text = text;[br]objselect.options.add(option);[br]}[br]//往前翻 year[br]function tiannetprevyear() {[br]if(tiannetyear > 999 && tiannetyear <10000){tiannetyear--;}[br]else{alert("年份超出范圍(1000-9999)!");}[br]tiannetsetday(tiannetyear,tiannetmonth);[br]//如果年份小于允許的最小年份,則創(chuàng)建對(duì)應(yīng)的option[br]if( tiannetyear < tiannetyearst ) {[br]tiannetyearst = tiannetyear;[br]createoption(document.all.seltianyear,tiannetyear,tiannetyear + "年");[br]}[br]checkselect(document.all.seltianyear,tiannetyear);[br]tiannetwritehead();[br]}[br]//往后翻 year[br]function tiannetnextyear() {[br]if(tiannetyear > 999 && tiannetyear <10000){tiannetyear++;}[br]else{alert("年份超出范圍(1000-9999)!");return;}[br]tiannetsetday(tiannetyear,tiannetmonth);[br]//如果年份超過允許的最大年份,則創(chuàng)建對(duì)應(yīng)的option[br]if( tiannetyear > tiannetyearend ) {[br]tiannetyearend = tiannetyear;[br]createoption(document.all.seltianyear,tiannetyear,tiannetyear + "年");[br]}[br]checkselect(document.all.seltianyear,tiannetyear);[br]tiannetwritehead();[br]}[br]//選擇今天[br]function tiannettoday() {[br]tiannetyear = tiannetdatenow.getfullyear();[br]tiannetmonth = tiannetdatenow.getmonth()+1;[br]tiannetday = tiannetdatenow.getdate();[br]tiannetsetvalue(true);[br]//tiannetsetday(tiannetyear,tiannetmonth);[br]//selectobject();[br]}[br]//往前翻月份[br]function tiannetprevmonth() {[br]if(tiannetmonth>1){tiannetmonth--}else{tiannetyear--;tiannetmonth=12;}[br]tiannetsetday(tiannetyear,tiannetmonth);[br]checksele
');[br]document.write(' 年');[br]document.write('');[br]document.write(' 月');[br]document.write('');[br]//document.write(' ');[br]//document.write('');[br]document.write(' ');[br]//輸出一條橫線[br]document.write('');[br]document.write(' ');[br]document.write('');[br]document.write(' ');[br]document.write('');[br]document.write('');[br]document.write('');[br]document.write(' ');[br]document.write('');[br]document.write('');[br]document.write(' ');[br]//輸出一條橫線[br]document.write('');[br]document.write('
該文章在 2010/7/19 9:16:07 編輯過 |
關(guān)鍵字查詢
相關(guān)文章
正在查詢... |