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