通用字节转换
function bytesToSize ( bytes) {
if ( bytes === 0 ) return "0 B" ;
var k = 1024 ;
var sizes = [ "B" , "KB" , "MB" , "GB" , "TB" , "PB" , "EB" , "ZB" , "YB" ] ;
var i = Math. floor ( Math. log ( bytes) / Math. log ( k) ) ;
return ( bytes / Math. pow ( k, i) ) . toPrecision ( 3 ) + " " + sizes[ i] ;
}
通用日期格式转换
function dateFormater ( formater, t) {
var date = t ? new Date ( t) : new Date ( ) ,
Y = date. getFullYear ( ) + "" ,
M = date. getMonth ( ) + 1 ,
D = date. getDate ( ) ,
H = date. getHours ( ) ,
m = date. getMinutes ( ) ,
s = date. getSeconds ( ) ;
return formater. replace ( /YYYY|yyyy/g , Y ) . replace ( /YY|yy/g , Y . substr ( 2 , 2 ) ) . replace ( /MM/g , ( M < 10 ? "0" : "" ) + M ) . replace ( /DD|dd/g , ( D < 10 ? "0" : "" ) + D ) . replace ( /HH|hh/g , ( H < 10 ? "0" : "" ) + H ) . replace ( /mm/g , ( m < 10 ? "0" : "" ) + m) . replace ( /ss/g , ( s < 10 ? "0" : "" ) + s) ;
}
两个日期相差天数
function dateDiff ( sDate1, sDate2) {
var aDate, oDate1, oDate2, iDays
aDate = sDate1. split ( "-" )
oDate1 = new Date ( aDate[ 1 ] + '-' + aDate[ 2 ] + '-' + aDate[ 0 ] )
aDate = sDate2. split ( "-" )
oDate2 = new Date ( aDate[ 1 ] + '-' + aDate[ 2 ] + '-' + aDate[ 0 ] )
iDays = parseInt ( Math. abs ( oDate1 - oDate2) / 1000 / 60 / 60 / 24 )
return iDays
}
获取浏览器信息
function getExplorerInfo ( ) {
var t = navigator. userAgent. toLowerCase ( ) ;
return 0 <= t. indexOf ( "msie" ) ? {
type: "IE" ,
version: Number ( t. match ( /msie ([\d]+)/ ) [ 1 ] )
} : ! ! t. match ( /trident\/.+?rv:(([\d.]+))/ ) ? {
type: "IE" ,
version: 11
} : 0 <= t. indexOf ( "edge" ) ? {
type: "Edge" ,
version: Number ( t. match ( /edge\/([\d]+)/ ) [ 1 ] )
} : 0 <= t. indexOf ( "firefox" ) ? {
type: "Firefox" ,
version: Number ( t. match ( /firefox\/([\d]+)/ ) [ 1 ] )
} : 0 <= t. indexOf ( "chrome" ) ? {
type: "Chrome" ,
version: Number ( t. match ( /chrome\/([\d]+)/ ) [ 1 ] )
} : 0 <= t. indexOf ( "opera" ) ? {
type: "Opera" ,
version: Number ( t. match ( /opera.([\d]+)/ ) [ 1 ] )
} : 0 <= t. indexOf ( "Safari" ) ? {
type: "Safari" ,
version: Number ( t. match ( /version\/([\d]+)/ ) [ 1 ] )
} : {
type: t,
version: - 1
} ;
是否为PC端浏览器模式
function isPCBroswer ( ) {
var e = navigator. userAgent. toLowerCase ( ) ,
t = "ipad" == e. match ( /ipad/i ) ,
i = "iphone" == e. match ( /iphone/i ) ,
r = "midp" == e. match ( /midp/i ) ,
n = "rv:1.2.3.4" == e. match ( /rv:1.2.3.4/i ) ,
a = "ucweb" == e. match ( /ucweb/i ) ,
o = "android" == e. match ( /android/i ) ,
s = "windows ce" == e. match ( /windows ce/i ) ,
l = "windows mobile" == e. match ( /windows mobile/i ) ;
return ! ( t || i || r || n || a || o || s || l) ;
}
指定范围随机数
function random ( lower, upper) {
lower = + lower || 0 ;
upper = + upper || 0 ;
return Math. random ( ) * ( upper - lower) + lower;
}
网址正则
function validURL ( url) {
var reg = /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/ ;
return reg. test ( url) ;
}
邮箱正则
function validEmail ( email) {
var reg = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/ ;
return reg. test ( email) ;
}
获取location指定值
function getQueryValueByName ( name) {
var q = window. location. search. match ( new RegExp ( "[?&]" + name + "=([^&#]*)" ) ) ;
return q && q[ 1 ] ;
}
获取location所有参数
function getQueryParam ( ) {
var q = { } ;
location. search. replace ( /([^?&=]+)=([^&]+)/g , function ( _, k, v) {
return q[ k] = v;
} ) ;
return q;
}