Calendar.js:
首頁 > web前端 > js教程 > JS日期與時間選擇控制項升級版(自寫)_javascript技巧

JS日期與時間選擇控制項升級版(自寫)_javascript技巧

WBOY
發布: 2016-05-16 17:27:08
原創
1130 人瀏覽過

鑑於網路上找到的幾個日期選擇程序有些問題,遂著手重寫一個程序,大部分還是藉鑑前人的代碼,添加了時間選擇功能,隱藏會遮擋控件的標籤select,object。
開始本想使用window.createPopup()來彈出日曆的選擇,這樣就可以跨越任何標籤。

不過做到中途發現用createPopup窗口實現理論上是行不通的:
一是因為不在窗體裡單擊任何地方都會關閉窗口,而當用下拉框選擇年份時,很有可能會點擊到窗體外的地方,當然這個可以自己寫select來避免,但是比較麻煩;
二是窗體的寬度和高度只能在彈出時設置,而顯然,在選擇不同年月時,控制高度是要改變的。

鑑於上述的原因,還是決定採用普通的處理方法。
JS日期與時間選擇控制項升級版(自寫)_javascript技巧 
Calendar.js:

複製程式碼 程式碼如下:

/**
*本日曆選擇控制由tiannet依據前人經驗完善而得。大部分程式碼來自meizz的日曆控制項。
*tiannet新增了時間選擇功能、select,object標籤隱藏功能,還有其它小功能。
*使用方法:
* (1)只選擇日期
* (2) 選擇日期和小時
* (3)選擇日期和小時及分鐘
*設定參數的方法
* (1)設定日期分隔符號setDateSplit(strSplit);預設為"-"
* (2)設定日期與時間之間的分隔符號setDateTimeSplit(strSplit);預設為" "
* (3)設定時間分隔符號setTimeSplit(strSplit);預設為":"
* (4)設定(1),(2) ,(3)中的分隔符號setSplit(strDateSplit,strDateTimeSplit,strTimeSplit);
* (5)設定開始與結束年份setYearPeriod(intDateBeg,intDateEnd)
*說明:
* 預設回傳的日期時間格式如同:2005-02-02 08:08
*/
//------------------ 樣式定義--------- ------------------//
//功能按鈕一樣樣式
var s_tiannet_turn_base = "height:16px;font-size:9pt;color:white; border:0 solid #CCCCCC;cursor:hand;background-color:#2650A6;";
//翻年、月等的按鈕
var s_tiannet_turn = "width:28px;" s_tiannet_turn_baseiannet_turn = "width:28px;" s_tiannet_turn_base; /關閉、清除等按鈕樣式
var s_tiannet_turn2 = "width:22px;" s_tiannet_turn_base;
//年選擇下拉框
var s_tiannet_select = "width:64px; //月、時、分選擇下拉框
var s_tiannet_select2 = "width:46px;display:none;";
//日期選擇控制體的樣式
var s_tiannet_body = "width:150;background -color:#2650A6;display:none;z-index:9998;position:absolute;"
"border-left:1 solid #CCCCCC;border-top:1 solid #CCCCCC;border-right:1 solid # 999999;border-bottom:1 solid #999999;";
//顯示日的td的樣式
var s_tiannet_day = "width:21px;height:20px;background-color:#D8F0FC;font-sizesize 10pt;";
//字體樣式
var s_tiannet_font = "color:#FFCC00;font-size:9pt;cursor:hand;";
//連結的樣式
var s_tiannet_link = " text-decoration:none;font-size:9pt;color:#2650A6;";
//橫線
var s_tiannet_line = "border-bottom:1 solid #6699CC";
//-- ---------------- 變數定義---------------------------//
var tiannetYearSt = 1950;//可選擇的開始年份
var tiannetYearEnd = 2010;//可選擇的結束年份
var tiannetDateNow = new Date();
var tiannetYear = tgetianDateNow. //定義年的變數的初始值
var tiannetMonth = tiannetDateNow.getMonth() 1; //定義月的變數的初始值
var tiannetDay = tiannetDateNow.getDate();
var tiannetDay = tiannetDateNow.getDate();
var tiannetHour = 8 ;//tiannetDateNow.getHours();
var tiannetMinute = 0;//tiannetDateNow.getMinutes();
var tiannetArrDay=new Array(42); //定義寫日期的陣列
varianDate=new Array(42); //定義寫日期的陣列
varianDateDate // "-"; //日期的分隔符號
var tiannetDateTimeSplit = " "; //日期與時間之間的分隔符號
var tiannetTimeSplit = ":"; //時間的分隔符號
var tiannetOutObject ; //接收日期時間的物件
var arrTiannetHide = new Array();//被強制隱藏的標籤
var m_bolShowHour = false;//是否顯示小時
var m_bolShowMinute = false;///是否顯示小時
var m_bolShowMinute = false;///是否顯示小時
var m_bolShowMinute = false;///是否顯示分鐘
var m_aMonHead = new Array(12); //定義陽曆中每個月的最大天數
m_aMonHead[0] = 31; m_aMonHead[1] = 28; m_aMonHead[2] = 31; m_aMonHead [3] = 30; m_aMonHead[4] = 31; m_aMonHead[5] = 30;
m_aMonHead[6] = 31; m_aMonHead[7] = 31; m_aMonHead[8] = 30; m_aMonHead[9] = 319] ; m_aMonHead[10] = 30; m_aMonHead[11] = 31;
// ---------------------- 使用者可呼叫的函數--- --------------------------//
//使用者主調函數-只選擇日期
function setDay(obj){
tiannetOutObject = obj;
//如果標籤中有值,則將日期初始化為目前值
var strValue = tiannetTrim(tiannetOutObject.value);
if( strValue != "" ){
tiannetInitDate(strValue);
}
tiannetPopCalendar();
}
//使用者主調函式-選擇日期和小時
function setDayH(Outobj){ m_bolShowHour = true;
//如果標籤中有值,則將日期和小時初始化為當前值
var strValue = tiannetTrim(tiannetOutObject.value);
if( strValue ! = "" ){
tiannetInitDate(strValue.substring(0,10));
var hour = strValue.substring(11,13);
if( hour }
tiannetPopCalendar(); }
//用户主调函数-选择日期和小时及分钟
function setDayHM(obj){
tiannetOutObject = obj;
m_bolShowHour = true;
m_bolShowMinute = true;
//如果标签中有值,则将日期和小时及分钟初始化为当前值
var strValue = tiannetTrim(tiannetOutObject.value);
if( strValue != "" ){
tiannetInitDate(strValue.substring(0,10));
var time = strValue.substring(11,16);
var arr = time.split(tiannetTimeSplit);
tiannetHour = arr[0];
tiannetMinute = arr[1];
if( tiannetHour < 10 ) tiannetHour = tiannetHour.substring(1,2);
if( tiannetMinute < 10 ) tiannetMinute = tiannetMinute.substring(1,2);
}
tiannetPopCalendar();
}
//设置开始日期和结束日期
function setYearPeriod(intDateBeg,intDateEnd){
tiannetYearSt = intDateBeg;
tiannetYearEnd = intDateEnd;
}
//设置日期分隔符。默认为"-"
function setDateSplit(strDateSplit){
tiannetDateSplit = strDateSplit;
}
//设置日期与时间之间的分隔符。默认为" "
function setDateTimeSplit(strDateTimeSplit){
tiannetDateTimeSplit = strDateTimeSplit;
}
//设置时间分隔符。默认为":"
function setTimeSplit(strTimeSplit){
tiannetTimeSplit = strTimeSplit;
}
//设置分隔符
function setSplit(strDateSplit,strDateTimeSplit,strTimeSplit){
tiannetDateSplit(strDateSplit);
tiannetDateTimeSplit(strDateTimeSplit);
tiannetTimeSplit(strTimeSplit);
}
//设置默认的日期。格式为:YYYY-MM-DD
function setDefaultDate(strDate){
tiannetYear = strDate.substring(0,4);
tiannetMonth = strDate.substring(5,7);
tiannetDay = strDate.substring(8,10);
}
//设置默认的时间。格式为:HH24:MI
function setDefaultTime(strTime){
tiannetHour = strTime.substring(0,2);
tiannetMinute = strTime.substring(3,5);
}
// ---------------------- end 用户可调用的函数 -----------------------------//
//------------------ begin 页面显示部分 ---------------------------//
var weekName = new Array("日","一","二","三","四","五","六");
document.write('
');
document.write('
');
document.write(''onclick="spanYearCEvent();"> 年');
document.write('');
document.write(''onclick="spanMonthCEvent();">  月');
document.write('');
//document.write('
');
//document.write('
');
document.write(''onclick="spanHourCEvent();"> 时');
document.write('');
document.write(''onclick="spanMinuteCEvent();">  分');
document.write('');
document.write('
');
//输出一条横线
document.write('
');
document.write('
');
document.write('');
document.write(' ');
document.write('');
document.write('');
document.write('
');
//输出一条横线
document.write('
');
document.write('');
document.write(' ');
for(var i =0;i < weekName.length;i ++){
//输出星期
document.write('');
}
document.write(' ');
document.write('
' + weekName[i] + '
');
//输出天的选择
document.write('');
var n = 0;
for (var i=0;i<5;i++) {
document.write (' ');
for (var j=0;j<7;j++){
document.write('');
n ++;
}
document.write (' ');
}
document.write (' ');
document.write('');
document.write('');
document.write('');
document.write (' ');
document.write('
'onClick="tiannetDay=this.innerText;tiannetSetValue(true);" '
+' style="' + s_tiannet_day + '"> 
+' style="' + s_tiannet_day + '"> +' style="' + s_tiannet_day + '"> 清空'+
关闭' +
确定 ' +
'
');
document.write('
');
//------------------ end 頁面顯示部分------------------- --------//
//------------------ 顯示日期時間的span標籤回應事件--------- ------------------//
//點選年份span標籤回應
function spanYearCEvent(){
hideElementsById(new Array("selTianYear" ,"tiannetMonthHead"),false);
if(m_bolShowHour) hideElementsById(new Array("tiannetHourHead"),false);
if(m_bolShowMinute) hideElementsByIdiana); 🎜>hideElementsById(new Array("tiannetYearHead","selTianMonth","selTianHour","selTianMinute"),true);
}
//點擊月份span標籤回應
function spanth/c
hideElementsById(new Array("selTianMonth","tiannetYearHead"),false);
if(m_bolShowHour) hideElementsById(new Array("tiannetHourHead"),false); Array("tiannetMinuteHead"),false);
hideElementsById(new Array("tiannetMonthHead","selTianYear","selTianHour","selTianMinute"),true);
}
}
/小時span標籤回應
function spanHourCEvent(){
hideElementsById(new Array("tiannetYearHead","tiannetMonthHead"),false);
if(m_bolShowHour) hElementElementsBy(newalse) ;
if(m_bolShowMinute) hideElementsById(new Array("tiannetMinuteHead"),false);
hideElementsById(new Array("tiannetHourHead","selTianYear","selTianMonth,","); 🎜>}
//點選分鐘span標籤回應
function spanMinuteCEvent(){
hideElementsById(new Array("tiannetYearHead","tiannetMonthHead"),false); 。 ","selTianHour"),true);
}
//依照標籤id隱藏或顯示標籤
function hideElementsById(arrId,bolHide){
var strDisplay = "";
if (bolHide) strDisplay = "none";
for(var i = 0;i var obj = document.getElementById(arrId[i]);
obj.style .display = strDisplay;
}
}
//------------------ end 顯示日期時間的span標籤回應事件----- ----------------------//
//判斷某年是否為閏年
function isPinYear(year){
var bolRet = false;
if (0==year%4&&((year 0!=0)||(year@0==0))) {
bolRet = true;
}
return bolRet;
}
//得到一個月的天數,閏年為29天
function getMonthCount(year,month){
var c=m_aMonHead[month-1];
if( (month==2)&&isPinYear(year)) c ;
return c;
}
//重新設定目前的日。主要在翻年、翻月時,當月大於當月的最大日
function setRealDayCount() {
if( tiannetDay > getMonthCount(tiannetYear,tiannetMonth) ) {
//如果目前的日大於當月的最大日,則取當月最大日
tiannetDay = getMonthCount(tiannetYear,tiannetMonth);
}
}
//在個位數前加零
function addZero(value ){
if(value value = "0" value;
}
return value;
}
//取出空格
function
}
//取出空格
function tiannetTrim(strian ) {
return str.replace(/(^s*)|(s*$)/g,"");
}
//為select建立一個option
function createOption(objSelect ,value,text){
var option = document.createElement("OPTION");
option.value = value;
option.text = text;
objSelect.options.add(option) ;
}
//往前翻Year
function tiannetPrevYear() {
if(tiannetYear > 999 && tiannetYear ;}
超出範圍(1000-9999)!
tiannetYearSt = tiannetYear;
createOption(document.all.selTianYear,tiannetYear,tiannetYear "年");
}
checkSelect(document.all.Tianpeads.T.TianY. ;
}
//往後翻Year
function tiannetNextYear() {
if(tiannetYear > 999 && tiannetYear else (1000-9999)! ");return;}
tiannetSetDay(tiannetYear,tiannetMonth);
//如果年份超過允許的最大年份,則建立對應的option
if( tiannetYear > tiannetYearEnd ) { tiannetYear = tiannetYear ;
createOption(document.all.selTianYear,tiannetYear,tiannetYear "年");
}
checkSelect(document.all.selTianYear,tiannetYear);
iannetWriteHead); 🎜>//選擇今天
function tiannetToday() {
tiannetYear = tiannetDateNow.getFullYear();
tiannetMonth = tiannetDateNow.getMonth() 1;tiannetSetValue(true);
//tiannetSetDay(tiannetYear,tiannetMonth);
//selectObject();
}
//往前翻月份
function tiannetPrevnetPrevmm >if(tiannetMonth>1){tiannetMonth--}else{tiannetYear--;tiannetMonth=12;}
tiannetSetDay(tiannetYear,tiannetMonth);
checkSelect(document.all.selTianMonth,tianMonth); tiannetWriteHead();
}
//往後翻月份
function tiannetNextMonth() {
if(tiannetMonth==12){tiannetYear ;tiannetMonth=1}else{tiannetMonth==12){tiannetYear ;tiannetMonth=1}else{tiannetMonth } (tiannetYear,tiannetMonth);
checkSelect(document.all.selTianMonth,tiannetMonth);
tiannetWriteHead();
}
//向span標籤中寫入年、月、時、分等年、月資料
function tiannetWriteHead(){
document.all.tiannetYearHead.innerText = tiannetYear "年";
document.all.tiannetMonthHead.innerText = tianMonth "bolth kianMonth "Hourumi%); all.tiannetHourHead.innerText = " " tiannetHour "時";
if( m_bolShowMinute ) document.all.tiannetMinuteHead.innerText = tiannetMinute "分數";
tianSetnet(Value);隱藏此控制項
}
//設定顯示天
function tiannetSetDay(yy,mm) {
setRealDayCount();//設定當月真實的日
tiannetWriteHead()
;
; var strDateFont1 = "", strDateFont2 = "" //處理日期顯示的風格
for (var i = 0; i var day1 = 1;
var firstday = new Date(yy,mm-1,1).getDay(); //某月第一天的星期幾
for (var i = firstday; day1 tiannetArrDay[i]=day1;day1 ;
}
//如果用於顯示日的最後一行的第一個單元格的值為空,則隱藏整行。
//if(tiannetArrDay[35] == ""){
// document.all.trTiannetDay5.style.display = "none";
//} else {
// document .all.trTiannetDay5.style.display = "";
//}
for (var i = 0; i var da = eval("document.all.tdTiannetDay" i) //書寫新的一個月的日期星期排列
if (tiannetArrDay[i]!="") {
//判斷是否為週末,如果是週末,則改為紅色字體
if(i % 7 == 0 || (i 1) % 7 == 0){
strDateFont1 = ""
strDateFont2 = "
"
} else {
strDateFont1 = "";
strDateFont2 = ""
}
da.innerHTML = strDateFont1 tiannetArrDay[i] strDateFont2;
//如果是目前選擇的天,則改變顏色
if(tiannetArrDay[i] == tiannetDay ) {
da.style.backgroundColor = "#CCCCCC";
} else {
da.style.backgroundor = "#EFEFEF";
}
da.style.cursor="hand"
} else {
da.innerHTML="";da.style.backgroundColor="";da.style.cursor="default"
}
}//end for
tiannetSetValue(false);//給文字方塊賦值,但不隱藏本控制
}//end function tiannetSetDay
//依據option的值選取option
function checkSelect(objSelect,selectValue) {
var count = parseInt(objSelect.length);
if( selectValue select selectValue.substring(1,2);
}
for(var i = 0;i if(objSelect.options[i].value == selectValue){
objSelect.selectedIndex = i;
break;
}
}//for
}
//選取年、月、時、分等下拉方塊
function selectObject() {
//如果年份小於允許的最小年份,則建立對應的option
if( tiannetYear for( var i = tiannetYear;i createOption (document.all.selTianYear,i,i "年");
}
tiannetYearSt = tiannetYear;
}
//如果年份超過允許的最大年份,則建立對應的option
if( tiannetYear > tiannetYearEnd ) {
for( var i = tiannetYearEnd 1;i createOption(document.all.selTianYear,i,i "年");
}
tiannetYearEnd = tiannetYear;
}
checkSelect(document.all.selTianYear,tiannetYear);
checkSelect(document.all.selTianMonth,tselTianMonth,MonnetMonth); all.selTianHour,tiannetHour);
if( m_bolShowMinute ) checkSelect(document.all.selTianMinute,tiannetMinute);
}
//給予接受日期時間的授權值function tiannetSetValue(bolHideControl){
var value = "";
if( !tiannetDay || tiannetDay == "" ){
tiannetOutObject.value = value;
}
var mm = tiannetMonth;
var day = tiannetDay;
if( mm if( day value = tiannetYear tiannetDateSplit mm tiannetDateSplit day;
if( m_bolShowHour ){ ;
if( hour value = tiannetDateTimeSplit hour;
}
if( m_bolShowhowMinute ){
var minute = tiannetMinute;
if( minute value = tiannetTimeSplit minute;
} //document.all.divTiannetDate.style.display = "none";
if( bolHideControl ) {
tiannetHideControl();
}
}
}
/是否顯示時間
function showTime(){
if( !m_bolShowHour && m_bolShowMinute){
alert("如果要選擇分鐘,則必須可以選擇小時!");
return;
}
hideElementsById(new Array("tiannetHourHead","selTianHour","tiannetMinuteHead","selTianMinute"),true);
if. >//顯示小時
hideElementsById(new Array("tiannetHourHead"),false);
}
if( m_bolShowMinute ){
//顯示器"),false);
}
}
//彈出顯示日曆選擇控件,以讓使用者選擇
function tiannetPopCalendar(){
//隱藏下拉框,顯示相對應的head
hideElementsById(new Array("selTianYear","selTianMonth","selTianHour","selTianMinute"),true);
hideElementsById(new Array("tiannetYearHead","tianthHead." tiannetMinuteHead"),false);
tiannetSetDay(tiannetYear,tiannetMonth);
tiannetWriteHead();
showTime();
vards = document.all.divTianstyleDate.all. , iY;
var h = document.all.divTiannetDate.offsetHeight;
var w = document.all.divTiannetDate.offsetWidth;
//計算left
if (windowoffsetWidth;
//計算left
if (windowoff.eventx h > document. .body.offsetWidth - 10 )
iX = window.event.x - h - 5 ;
else
iX = window.event.x 5;
if (iX iX=0;
//計算top
iY = window.event.y;
if (window.event.y w > document.body.offsetHeight - 10 )
iY = document.body. scrollTop document.body.offsetHeight - w - 5 ;
else
iY = document.body.scrollTop window.event.y 5;
if (iY iY=0; >dads.left = iX;
dads.top = iY;
tiannetShowControl();
selectObject();
}
//隱藏日曆控制項(同時顯示被強制隱藏的標籤)
function tiannetHideControl(){
document.all.divTiannetDate.style.display = "none";
tiannetShowObject();
arrTiannetHide = new Array();//將隱藏的標籤物件清空
}
//顯示日曆控制項(同時隱藏會遮擋的標籤)
function tiannetShowControl(){
document.all.divTiannetDate.style.display = "";
tiannetHideObject ("SELECT");
tiannetHideObject("OBJECT");
}
//依照標籤名稱隱藏標籤。如會遮住控制項的select,object
function tiannetHideObject(strTagName) {
x = document.all.divTiannetDate.offsetLeft;
y = document.all.divTiannetDate.offsetLeft;
y = document.all.divTiannetDate.offsetLeft;
y = document.all.divTiannetDate.offsetLeft; .all.divTiannetDate.offsetHeight;
w = document.all.divTiannetDate.offsetWidth;
for (var i = 0; i { 🎜>var obj = document.all.tags(strTagName)[i];
if (! obj || ! obj.offsetParent)
continue;
// 取得元素對於BODY標記的相對座標
var objLeft = obj.offsetLeft;
var objTop = obj.offsetTop;
var objHeight = obj.offsetHeight;
var objWidth = obj.obj.dobj. 🎜>while (objParent.tagName.toUpperCase() != "BODY"){
objLeft = objParent.offsetLeft;
objTop = objParent.offsetTop; obParent = objTop = objParent.offsetTop; 🎜>//alert("控制項左端:" x "select左端" (objLeft objWidth) "控制項底部:" (y h) "select高:" objTop);
var bolHide = true;
if( obj .style.display == "none" || obj.style.visibility == "hidden" || obj.getAttribute("Author") == "tiannet" ){
//如果標籤本身就是隱藏的,則不需要再隱藏。如果是控制項中的下拉框,也不用隱藏。
bolHide = false;
}
if( ( (objLeft objWidth) > x && (y h 20) > objTop && (objTop objHeight) > y && objLeft //arrTiannetHide.push(obj);//記錄被隱藏的標籤物件
arrTiannetHide[arrTiannetHide.length] = obj;
obj.style.visibility = "hidden";
}
}
}
}
//顯示被隱藏的標籤
function tiannetShowObject(){
for(var i = 0;i //alert(arrTiannetHide.length;i ){
//alert(arrTiannetHide [i]);
arrTiannetHide[i].style.visibility = "";
}
}
//初始化日期。
function tiannetInitDate(strDate){
var arr = strDate.split(tiannetDateSplit);
tiannetYear = arr[0];
tiannetMonth = arr[1]; ];
}
//清空
function tiannetClear(){
tiannetOutObject.value = "";
tiannetHideControl();
}
//任意點擊時關閉時關閉時關閉時關閉此控制項
function document.onclick(){
with(window.event.srcElement){
if (tagName != "INPUT" && getAttribute("Author") != "tiannet")
tiannetHideControl();
}
}
//按ESC鍵關閉該控制項
function document.onkeypress(){
if( event.keyCode == 27 ){
tiannetHideControl();
}
}


Calendar.html:



複製程式碼

複製程式碼複製程式碼
複製程式碼> 程式碼如下:




標題>




頭>

(1)只選日期
;
(2)選擇日期和小時

(3)選擇日期和小時及分鐘
身體>

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板