javascript 自定義常用方法
更新時(shí)間:2009年08月21日 01:32:50 作者:
在實(shí)際的js開發(fā)過程中,我們常常會(huì)有相似或相同的需求。這時(shí)候如果沒有很好的封裝(通用功能),代碼的重復(fù)將不可避免。
三、日期處理函數(shù)
日期處理也是js的一個(gè)重要方面,而且比較容易出現(xiàn)意想不到的錯(cuò)誤或者bug。下面就是收集整理的需要注意的一些常見函數(shù)(封裝成一個(gè)時(shí)間類):
// 日期對(duì)象
function PowerDate() {
this .date = null ;
// 格式化時(shí)是否加零補(bǔ)位標(biāo)志
this .isFmtZero = false ;
this .weekArr = [[ " 星期日 " , " 星期一 " , " 星期二 " , " 星期三 " , " 星期四 " , " 星期五 " , " 星期六 " ],
[ " SUN " , " MON " , " TUR " , " WED " , " THU " , " FRI " , " SAT " ]];
this .monthArr = [ " JAN " , " FEB " , " MAR " , " APR " , " MAY " , " JUN " , " JUL " , " AUG " , " SEP " , " OCT " , " NOV " , " DEC " ];
// 初始化日期對(duì)象
switch (arguments.length) {
case 0 :
this .date = new Date();
break ;
case 1 :
var reg = / ^(\d{2,4})\D+(\d{1,2})\D+(\d{1,2})$ / ;
var str = arguments[ 0 ].replace( / \s / , "" );
str = str.replace(reg, " $1/$2/$3 " );
this .date = new Date(str);
break ;
case 3 :
this .date = new Date(arguments[ 0 ], arguments[ 1 ] - 1 , arguments[ 2 ]);
break ;
case 6 :
this .date = new Date(arguments[ 0 ], arguments[ 1 ] - 1 , arguments[ 2 ], arguments[ 3 ], arguments[ 4 ], arguments[ 5 ]);
break ;
case 7 :
this .date = new Date(arguments[ 0 ], arguments[ 1 ] - 1 , arguments[ 2 ], arguments[ 3 ], arguments[ 4 ], arguments[ 5 ], arguments[ 6 ]);
break ;
default : this .date = new Date( " 1970/1/1 " ); break ;
}
// 初始化失敗處理
if ( typeof ( this .date) != " object " || ! ( / Date / .test( this .date.constructor)))
throw ( new Error( - 1 , ' 構(gòu)造PowerDate方法失敗,檢查輸入?yún)?shù)! ' ));
this .getDate = function () {
return this .date;
}
this .getFullYear = function () {
return this .date.getFullYear();
};
this .getYear = function () {
return this .date.getYear();
};
this .getMonth = function () {
return this .frmWithZero( this .date.getMonth() + 1 );
};
this .getDay = function () {
return this .frmWithZero( this .date.getDate());
};
this .getHour = function () {
return this .frmWithZero( this .date.getHours());
};
this .getMinute = function () {
return this .frmWithZero( this .date.getMinutes());
};
this .getSecond = function () {
return this .frmWithZero( this .date.getSeconds());
};
this .getMillisecond = function () {
var ss = this .date.getMilliseconds();
if ( this .isFmtZero == true && ss < 10 )
return " 00 " + ss;
else if ( this .isFmtZero == true && ss < 100 )
return " 0 " + ss;
else return ss;
};
this .getWeek = function () {
return this .date.getDay();
};
this .setIsFmtZero = function (val) {
this .isFmtZero = val;
};
this .frmWithZero = function (num) {
if ( this .isFmtZero == true && num < 10 )
return " 0 " + num;
else return num;
}
/*
功能:根據(jù)輸入表達(dá)式返回日期字符串
參數(shù):dateFmt:字符串,由以下結(jié)構(gòu)組成 yy:長寫年,YY:短寫年mm:數(shù)字月,MM:英文月,dd:日,hh:時(shí),mi:分,ss秒,ms:毫秒,we:漢字星期,WE:英文星期.
isFmtZero:布爾值,true:需要用0補(bǔ)位,false:不需要用0補(bǔ)位
*/
this .getString = function (dateFmt) {
if ( typeof (dateFmt) != " string " )
throw ( new Error( - 1 , ' getString()方法需要字符串類型參數(shù)! ' ));
var str = dateFmt;
str = str.replace( / yy / g, this .getFullYear());
str = str.replace( / YY / g, this .getYear());
str = str.replace( / mm / g, this .getMonth());
str = str.replace( / MM / g, this .monthArr[ this .getMonth() - 1 ]);
str = str.replace( / dd / g, this .getDay());
str = str.replace( / hh / g, this .getHour());
str = str.replace( / mi / g, this .getMinute());
str = str.replace( / ss / g, this .getSecond());
str = str.replace( / ms / g, this .getMillisecond());
str = str.replace( / we / g, this .weekArr[ 0 ][ this .getWeek()]);
str = str.replace( / WE / g, this .weekArr[ 1 ][ this .getWeek()]);
return str;
};
/* 功能 : 返回與某日期相距N天(N個(gè)24小時(shí))的日期
* 參數(shù) : num number類型 可以為正負(fù)整數(shù)或者浮點(diǎn)數(shù)
* 返回 : 新的PowerDate類型
* 方法 : powerDate.dateAfterDays(num);
*/
this .dateAfterDays = function (num) {
if ( typeof (num) != " number " ) throw new Error( - 1 , " dateAfterDays(num)參數(shù)為數(shù)值類型. " );
var dd = this .date.valueOf();
dd += num * 24 * 3600 * 1000 ;
this .date = new Date(dd);
return this ;
};
/* 功能 : 返回與某日期相距N秒的日期
* 參數(shù) : num number類型 可以為正負(fù)整數(shù)或者浮點(diǎn)數(shù)
* 返回 : 新的日期
* 方法 : powerDate.dateAfterDays(num);
*/
this .dateAfterSeconds = function (num) {
if ( typeof (num) != " number " ) throw new Error( - 1 , " dateAfterDays(num)參數(shù)為數(shù)值類型. " );
var dd = this .date.valueOf();
dd += num * 1000 ;
this .date = new Date(dd);
return this ;
};
// 判斷是否是閏年
this .isLeapYear = function () {
var year = this .getFullYear();
return ( 0 == year % 4 && ((year % 100 != 0 ) || (year % 400 == 0 )));
};
// 返回該月天數(shù)
this .getDaysOfMonth = function () {
return ( new Date( this .getFullYear(), this .getMonth(), 0 )).getDate();
};
// 轉(zhuǎn)換成大寫日期(中文)
this .getChinaDate = function () {
var year = this .getFullYear();
var month = this .getMonth();
var day = this .getDay();
var arrNum = [ " 零 " , " 一 " , " 二 " , " 三 " , " 四 " , " 五 " , " 六 " , " 七 " , " 八 " , " 九 " , " 十 " , " 十一 " , " 十二 " ];
var strTmp = "" ;
for ( var i = 0 , j = year.toString().length; i < j; i ++ ) {
strTmp += arrNum[year.toString().charAt(i)];
}
strTmp += " 年 " ;
if (month.toString().substr( 0 , 1 ) == 0 ) {
strTmp += arrNum[parseInt(month.toString().substr( 1 ), 10 )] + " 月 " ;
}
else
strTmp += arrNum[month] + " 月 " ;
if (day < 10 )
strTmp += arrNum[parseInt(day.toString().substr( 1 ), 10 )];
else if (day < 20 )
strTmp += " 十 " + arrNum[day - 10 ];
else if (day < 30 )
strTmp += " 二十 " + arrNum[day - 20 ];
else
strTmp += " 三十 " + arrNum[day - 30 ];
strTmp += " 日 " ;
return strTmp;
};
// 日期比較函數(shù),如大于參數(shù):1,相等:0 不等: -1
this .dateCompare = function (dat) {
if ( typeof (dat) == " string " ) {
if (dat != "" ) dat = new Date(timeString);
else dat = new Date();
}
if ( typeof (dat) != " object " || ! ( / Date / .test( this .date.constructor))) {
throw new Error( - 2 , " dateCompare的參數(shù)為日期類型或者可直接轉(zhuǎn)化為日期類型的字符串! " );
}
var d = this .date.getTime() - dat.getTime();
return d > 0 ? 1 : (d == 0 ? 0 : - 1 );
};
/* 功能:返回兩日期之差
*參數(shù):pd PowerDate對(duì)象
* type: 返回類別標(biāo)識(shí).yy:年,mm:月,dd:日,hh:小時(shí),mi:分,ss:秒,ms:毫秒
* intOrFloat :返回整型還是浮點(diǎn)型值 0:整型,1:浮點(diǎn)型
*/
this .calDateDistance = function (pd, type, intOrFloat) {
var miSecMain = this .date.valueOf();
var miSecSub = pd.getDate().valueOf();
var num = 0 ;
switch (type) {
case " yy " : num = this .getFullYear() - pd.getFullYear();
break ;
case " mm " : num = ( this .getFullYear() - pd.getFullYear()) * 12 + this .getMonth() - pd.getMonth();
break ;
case " dd " : num = this .fmtRtnVal((miSecMain - miSecSub) / 86400000 , intOrFloat);
break ;
case " hh " : num = this .fmtRtnVal((miSecMain - miSecSub) / 3600000 , intOrFloat);
break ;
case " mi " : num = this .fmtRtnVal((miSecMain - miSecSub) / 60000 , intOrFloat);
break ;
case " ss " : num = this .fmtRtnVal((miSecMain - miSecSub) / 1000 , intOrFloat);
break ;
case " ms " : num = (miSecMain - miSecSub);
break ;
default :
throw new Error( - 1 , " 沒有您要求返回的類型,請(qǐng)檢查輸入?yún)?shù)! " );
break ;
}
return num;
};
this .fmtRtnVal = function (val, intOrFloat) {
// alert(val);
return (intOrFloat == 0 ? Math.floor(val) : parseInt(val * 100 ) / 100 );
};
}
// 測(cè)試
function test() {
var d = new PowerDate( " 1998/7/3 " ); // 實(shí)例化一個(gè)PowerDate對(duì)象
d.setIsFmtZero( true ); // 設(shè)置為用0補(bǔ)位輸出
alert(d.getString( " yy-mm-dd hh:mi:ss.ms we " )); // 輸出日期字符串
var d2 = new PowerDate(); // 實(shí)例化一個(gè)PowerDate對(duì)象
alert(d2.calDateDistance( new PowerDate( " 2005/7/31 " ), " yy " , 1 )); // 輸出日期字符串
alert(d.getChinaDate());
alert(d2.dateAfterDays( 3 ).getFullYear());
}
相關(guān)文章
echarts同一頁面中四個(gè)圖表切換的js數(shù)據(jù)交互方法示例
這篇文章主要給大家介紹了關(guān)于echarts同一頁面中四個(gè)圖表切換的js數(shù)據(jù)交互的相關(guān)資料,文中給出了完整的示例代碼供大家參考學(xué)習(xí),對(duì)大家的學(xué)習(xí)或者工作具有一定的幫助,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07
JavaScript逆向調(diào)試技巧總結(jié)分享
當(dāng)我們抓取網(wǎng)頁端數(shù)據(jù)時(shí),經(jīng)常被加密參數(shù)、加密數(shù)據(jù)所困擾,如何快速定位這些加解密函數(shù),尤為重要,下面這篇文章主要給大家介紹了關(guān)于JavaScript逆向調(diào)試技巧的相關(guān)資料,需要的朋友可以參考下2022-06-06
input file上傳 圖片預(yù)覽功能實(shí)例代碼
input file上傳圖片預(yù)覽其實(shí)很簡單。今天小編就通過本文給大家介紹input file上傳 圖片預(yù)覽功能的實(shí)現(xiàn)代碼,比較簡單,對(duì)input file 上傳預(yù)覽功能感興趣的朋友參考下吧2016-10-10
JavaScript基于DOM操作實(shí)現(xiàn)簡單的數(shù)學(xué)運(yùn)算功能示例
這篇文章主要介紹了JavaScript基于DOM操作實(shí)現(xiàn)簡單的數(shù)學(xué)運(yùn)算功能,涉及javascript節(jié)點(diǎn)操作、元素遍歷及數(shù)學(xué)運(yùn)算相關(guān)操作技巧,需要的朋友可以參考下2017-01-01
JavaScript實(shí)現(xiàn)一個(gè)空中避難的小游戲
最近利用Javascript實(shí)現(xiàn)了一個(gè)小游戲,覺著還不錯(cuò),所以分享給大家,下面這篇文章主要給大家介紹了利用JavaScript實(shí)現(xiàn)一個(gè)空中避難的小游戲的相關(guān)資料,文中給出了完整的示例代碼供大家參考學(xué)習(xí),需要的朋友們下面來一起看看吧。2017-06-06
JS Ajax請(qǐng)求會(huì)話過期處理問題解決方法分析
這篇文章主要介紹了JS Ajax請(qǐng)求會(huì)話過期處理問題解決方法,結(jié)合實(shí)例形式簡單分析了ajax請(qǐng)求會(huì)話過期處理的相關(guān)原理、解決方法及操作注意事項(xiàng),需要的朋友可以參考下2019-11-11
淺談js的html元素的父節(jié)點(diǎn),子節(jié)點(diǎn)
下面小編就為大家?guī)硪黄獪\談js的html元素的父節(jié)點(diǎn),子節(jié)點(diǎn)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-08-08

