javascript 自定义常用方法

 更新时间:2009年08月21日 01:32:50   作者:  
在实际的js开发过程中,我们常常会有相似或相同的需求。这时候如果没有很好的封装(通用功能),代码的重复将不可避免。

三、日期处理函数
日期处理也是js的一个重要方面,而且比较容易出现意想不到的错误或者bug。下面就是收集整理的需要注意的一些常见函数(封装成一个时间类):

// 日期对象
function PowerDate() {
this .date = null ;
// 格式化时是否加零补位标志
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 " ];
// 初始化日期对象
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 , ' 构造PowerDate方法失败,检查输入参数! ' ));

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;
}
/*
功能:根据输入表达式返回日期字符串
参数:dateFmt:字符串,由以下结构组成 yy:长写年,YY:短写年mm:数字月,MM:英文月,dd:日,hh:时,mi:分,ss秒,ms:毫秒,we:汉字星期,WE:英文星期.
isFmtZero:布尔值,true:需要用0补位,false:不需要用0补位
*/
this .getString = function (dateFmt) {
if ( typeof (dateFmt) != " string " )
throw ( new Error( - 1 , ' getString()方法需要字符串类型参数! ' ));
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个24小时)的日期
* 参数 : num number类型 可以为正负整数或者浮点数
* 返回 : 新的PowerDate类型
* 方法 : powerDate.dateAfterDays(num);
*/
this .dateAfterDays = function (num) {
if ( typeof (num) != " number " ) throw new Error( - 1 , " dateAfterDays(num)参数为数值类型. " );
var dd = this .date.valueOf();
dd += num * 24 * 3600 * 1000 ;
this .date = new Date(dd);
return this ;
};
/* 功能 : 返回与某日期相距N秒的日期
* 参数 : num number类型 可以为正负整数或者浮点数
* 返回 : 新的日期
* 方法 : powerDate.dateAfterDays(num);
*/
this .dateAfterSeconds = function (num) {
if ( typeof (num) != " number " ) throw new Error( - 1 , " dateAfterDays(num)参数为数值类型. " );
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 )));
};

// 返回该月天数
this .getDaysOfMonth = function () {
return ( new Date( this .getFullYear(), this .getMonth(), 0 )).getDate();
};

// 转换成大写日期(中文)
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;
};

// 日期比较函数,如大于参数: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的参数为日期类型或者可直接转化为日期类型的字符串! " );
}
var d = this .date.getTime() - dat.getTime();
return d > 0 ? 1 : (d == 0 ? 0 : - 1 );
};

/* 功能:返回两日期之差
*参数:pd PowerDate对象
* type: 返回类别标识.yy:年,mm:月,dd:日,hh:小时,mi:分,ss:秒,ms:毫秒
* intOrFloat :返回整型还是浮点型值 0:整型,1:浮点型
*/
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 , " 没有您要求返回的类型,请检查输入参数! " );
break ;
}
return num;
};
this .fmtRtnVal = function (val, intOrFloat) {
// alert(val);
return (intOrFloat == 0 ? Math.floor(val) : parseInt(val * 100 ) / 100 );
};
}

// 测试
function test() {
var d = new PowerDate( " 1998/7/3 " ); // 实例化一个PowerDate对象
d.setIsFmtZero( true ); // 设置为用0补位输出
alert(d.getString( " yy-mm-dd hh:mi:ss.ms we " )); // 输出日期字符串

var d2 = new PowerDate(); // 实例化一个PowerDate对象
alert(d2.calDateDistance( new PowerDate( " 2005/7/31 " ), " yy " , 1 )); // 输出日期字符串
alert(d.getChinaDate());
alert(d2.dateAfterDays( 3 ).getFullYear());
}

相关文章

最新评论