1.js常用工具类
/**
* js常用工具类
*/
/**
* 方法作用:【格式化时间】
* 使用方法
* 示例:
* 使用方式一:
* var now = new Date();
* var nowStr = now.dateFormat("yyyy-MM-dd hh:mm:ss");
* 使用方式二:
* new Date().dateFormat("yyyy年MM月dd日");
* new Date().dateFormat("MM/dd/yyyy");
* new Date().dateFormat("yyyyMMdd");
* new Date().dateFormat("yyyy-MM-dd hh:mm:ss");
* @param format {date} 传入要格式化的日期类型
* @returns {2015-01-31 16:30:00}
*/
Date.prototype.dateFormat = function (format){
var o = {
"M+" : this.getMonth()+1, //month
"d+" : this.getDate(), //day
"h+" : this.getHours(), //hour
"m+" : this.getMinutes(), //minute
"s+" : this.getSeconds(), //second
"q+" : Math.floor((this.getMonth()+3)/3), //quarter
"S" : this.getMilliseconds() //millisecond
}
if(/(y+)/.test(format)) {
format = format.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
}
for(var k in o) {
if(new RegExp("("+ k +")").test(format)) {
format = format.replace(RegExp.$1, RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));
}
}
return format;
}
/***********************************************************************
* 日期时间工具类 *
* 注:调用方式,deteUtil.方法名 *
* ********************************************************************/
var dateUtil = {
/*
* 方法作用:【取传入日期是星期几】
* 使用方法:dateUtil.nowFewWeeks(new Date());
* @param date{date} 传入日期类型
* @returns {星期四,...}
*/
nowFewWeeks:function(date){
if(date instanceof Date){
var dayNames = new Array("星期天","星期一","星期二","星期三","星期四","星期五","星期六");
return dayNames[date.getDay()];
} else{
return "Param error,date type!";
}
},
/*
* 方法作用:【字符串转换成日期】
* 使用方法:dateUtil.strTurnDate("2010-01-01");
* @param str {String}字符串格式的日期,传入格式:yyyy-mm-dd(2015-01-31)
* @return {Date}由字符串转换成的日期
*/
strTurnDate:function(str){
var re = /^(\d{4})\S(\d{1,2})\S(\d{1,2})$/;
var dt;
if (re.test(str)){
dt = new Date(RegExp.$1,RegExp.$2 - 1,RegExp.$3);
}
return dt;
},
/*
* 方法作用:【计算2个日期之间的天数】
* 传入格式:yyyy-mm-dd(2015-01-31)
* 使用方法:dateUtil.dayMinus(startDate,endDate);
* @startDate {Date}起始日期
* @endDate {Date}结束日期
* @return endDate - startDate的天数差
*/
dayMinus:function(startDate, endDate){
if(startDate instanceof Date && endDate instanceof Date){
var days = Math.floor((endDate-startDate)/(1000 * 60 * 60 * 24));
return days;
}else{
return "Param error,date type!";
}
}
};
/***********************************************************************
* 加载工具类 *
* 注:调用方式,loadUtil.方法名 *
* ********************************************************************/
var loadUtil = {
/*
* 方法说明:【动态加载js文件css文件】
* 使用方法:loadUtil.loadjscssfile("http://libs.baidu.com/jquery/1.9.1/jquery.js","js")
* @param fileurl 文件路径,
* @param filetype 文件类型,支持传入类型,js、css
*/
loadjscssfile:function(fileurl,filetype){
if(filetype == "js"){
var fileref = document.createElement('script');
fileref.setAttribute("type","text/javascript");
fileref.setAttribute("src",fileurl);
}else if(filetype == "css"){
var fileref = document.createElement('link');
fileref.setAttribute("rel","stylesheet");
fileref.setAttribute("type","text/css");
fileref.setAttribute("href",fileurl);
}
if(typeof fileref != "undefined"){
document.getElementsByTagName("head")[0].appendChild(fileref);
}else{
alert("loadjscssfile method error!");
}
}
};
/***********************************************************************
* 字符串操作工具类 *
* 注:调用方式,strUtil.方法名 *
* ********************************************************************/
var strUtil = {
/*
* 判断字符串是否为空
* @param str 传入的字符串
* @returns {}
*/
isEmpty:function(str){
if(str != null && str.length > 0){
return true;
}else{
return false;
}
},
/*
* 判断两个字符串子否相同
* @param str1
* @param str2
* @returns {Boolean}
*/
isEquals:function(str1,str2){
if(str1==str2){
return true;
}else{
return false;
}
},
/*
* 忽略大小写判断字符串是否相同
* @param str1
* @param str2
* @returns {Boolean}
*/
isEqualsIgnorecase:function(str1,str2){
if(str1.toUpperCase() == str2.toUpperCase()){
return true;
}else{
return false;
}
},
/**
* 判断是否是数字
* @param value
* @returns {Boolean}
*/
isNum:function (value){
if( value != null && value.length>0 && isNaN(value) == false){
return true;
}else{
return false;
}
},
/**
* 判断是否是中文
* @param str
* @returns {Boolean}
*/
isChine:function(str){
var reg = /^([u4E00-u9FA5]|[uFE30-uFFA0])*$/;
if(reg.test(str)){
return false;
}
return true;
};
2.封装自己常用的工具类
/**
* @author:水痕
* @timer:2016-07-28
* @email:[email protected]
* @version:1.0
* @title:封装一个自己常用的工具类js
* @note:
*/
;
(function(window, document, $) {
window.utils = {
/关于数组操作js开始/
tmArray: {
/*each和map的功能是一样的*/
each: function(arr, fn) {
fn = fn || Function;
var a = [];
var args = Array.prototype.slice.call(arguments, 1);
for(var i = 0; i < arr.length; i++) {
var res = fn.apply(arr, [arr[i], i].concat(args));
if(res != null) a.push(res);
}
return a;
},
/*each和map的功能是一样的*/
map: function(arr, fn, thisObj) {
var scope = thisObj || window;
var a = [];
for(var i = 0, j = arr.length; i < j; ++i) {
var res = fn.call(scope, arr[i], i, this);
if(res != null) a.push(res);
}
return a;
},
/**
* 数组的排序
* @param array
* @param sortFlag
* @returns {*}
*/
orderBy: function(array, sortFlag) {
var $arr = array;
if(sortFlag == 'asc') {
$arr.sort(this._numAscSort);
} else if(sortFlag == 'desc') {
$arr.sort(this._numDescSort);
} else {
$arr.sort(this._numAscSort);
}
return $arr;
},
// 求两个集合的并集
union: function(a, b) {
var newArr = a.concat(b);
return this.unique2(newArr);
},
// 求两个集合的补集
complement: function(a, b) {
return this.minus(this.union(a, b), this.intersect(a, b));
},
// 求两个集合的交集
intersect: function(a, b) {
a = this.unique(a);
return this.each(a, function(o) {
return b.contains(o) ? o : null;
});
},
//求两个集合的差集
minus: function(a, b) {
a = this.unique(a);
return this.each(a, function(o) {
return b.contains(o) ? null : o;
});
},
/**
* 数组的去重
* @param arr
* @returns {Array}
*/
unique: function(arr) {
var ra = new Array();
for(var i = 0; i < arr.length; i++) {
if(!ra.contains(arr[i])) {
//if(this.contains(ra,arr[i])){
ra.push(arr[i]);
}
}
return ra;
},
/**
* 数组的去重复
* @param arr
* @returns {*}
*/
unique2: function(arr) {
for(var i = 0; i < arr.length; i++) {
for(var j = i + 1; j < arr.length;) {
if(arr[j] == arr[i]) {
arr.splice(j, 1);
} else {
j++;
}
}
}
return arr;
},
/**
* 数组去除重复的(根据对象来)
* @param {Object} ary
*/
unique3: function(ary) {
var result = [],
hash = {};
for(var i = 0, elem;
(elem = arr[i]) != null; i++) {
if(!hash[elem]) {
result.push(elem);
hash[elem] = true;
}
}
return result;
},
/**
* 获取数组的下标
* @param arr
* @param val
* @returns {number}
*/
indexOf: function(arr, val) {
for(var i = 0; i < arr.length; i++) {
if(arr[i] == val) {
return i;
}
}
return -1;
},
/**
* 判断一个元素是否在一个数组中
* @param arr
* @param val
* @returns {boolean}
*/
contains: function(arr, val) {
return this.indexOf(arr, val) != -1 ? true : false;
},
/**
* 数组中删除一个元素
* @param arr
* @param indexs
* @returns {*}
*/
remove: function(arr, indexs) {
var index = this.indexOf(arr, indexs);
if(index > -1) {
arr.splice(index, 1);
}
return arr;
},
removeObject: function(arr, item) {
for(var i = 0; i < arr.length; i++) {
var jsonData = arr[i];
for(var key in jsonData) {
if(jsonData[key] == item) {
arr.splice(i, 1);
}
}
}
return arr;
},
/**
* 求数组中最大值
* @param arr
* @returns {number|Number}
*/
arrMax: function(arr) {
return Math.max.apply(null, arr);
},
/**
* 求数组中最小值
* @param arr
* @returns {number|Number}
*/
arrMin: function(arr) {
return Math.min.apply(null, arr);
},
/**
* 删除数组元素的方法
*/
removeAry: function(ary, ele) {
ary.splice(ary.indexOf(ele), 1);
},
/**
* 将类数组转换为数组的方法
* @param ary
* @returns {Array}
*/
formArray: function(ary) {
var arr = [];
if(Array.isArray(ary)) {
arr = ary;
} else {
arr = Array.prototype.slice.call(ary);
};
return arr;
},
/**
* 定义一个数组排序的方法
* 默认为升序排序asc,
* 如果传递是参数是一个的话,那么就是是升序,如果传递的参数是两个的话,如果第一个参数不能转换为数组的话,也直接退出
* 参数:acs:表示升序
* 参数:desc:表示降序
* @returns {*}
*/
arrySort: function() {
var arg = arguments;
var len = arg.length;
var ary = this.arryList(arg[0]);
//如果没传递参数,或者传递的不能转换为数组的话就直接返回
if(!len || Array.isArray(ary) == false) {
return false;
};
if(len == 1) {
return ary.sort(function(a, b) {
return a - b;
});
} else {
return ary.sort(function(a, b) {
if(arg[1] == "desc") {
return b - a;
} else if(arg[1] == "asc") {
return a - b;
} else {
return a - b;
};
});
};
},
/**
* 求和函数
* @param arr
* @returns {number}
*/
arySum: function(arr) {
var ary = [];
var result = 0;
if(arr instanceof Array) {
ary = arr;
} else {
ary = this.formArray(arr);
};
for(var i = 0; i < ary.length; i++) {
result += parseFloat(ary[i]);
};
return result;
},
/**
* 数组随机排列
* @param {Object} ary
*/
shuffle: function(ary) {
var input = this;
for(var i = input.length - 1; i >= 0; i--) {
var randomIndex = Math.floor(Math.random() * (i + 1));
var itemAtIndex = input[randomIndex];
input[randomIndex] = input[i];
input[i] = itemAtIndex;
}
return input;
},
/**
* 数组随机排序
* @param {Object} target
*/
shuffle1: function(target) {
function randomsort(a, b) {
return Math.random() > .5 ? -1 : 1;
//用Math.random()函数生成0~1之间的随机数与0.5比较,返回-1或1
}
return target.sort(randomsort);
},
/**
* 判断是不是数组
* @param {Object} ary
*/
isArray: function(ary) {
var objectToStringFn = Object.prototype.toString;
var arrayToStringResult = objectToStringFn.call([]);
return function(subject) {
return objectToStringFn.call(subject) === arrayToStringResult;
};
},
/**
* 随机返回数组中一个元素
* @param {Object} ary
*/
randomItem: function(ary) {
return ary[Math.ceil(Math.random() * ary.length)];
},
/**
* 判断数组中是否包含某一项
* @param arr
* @returns {number|Number}
* 调用方法:var max = utils.arrContains([],"",false) flag 如果为true,则判断字符串 false则判断字符
*/
arrContains: function(arr, str, flag) {
if(flag) {
if(arr.length > 0 && this.isNotEmpty(str)) {
for(var i = 0; i < arr.length; i++) {
if(arr[i] == str) {
return true;
} else {
return false;
}
}
}
} else {
for(var i = 0; i < arr.length; i++) {
for(var j = 0; j < arr[i].length; j++) {
if(arr[i].charAt(j) == str) {
return true;
} else {
false;
}
}
}
}
},
/**
* 判断数组是否有重复的项
* @param {Object} arr
*/
isRepeat: function(arr) { //arr是否有重复元素
var hash = {};
for(var i in arr) {
if(hash[arr[i]]) return true;
hash[arr[i]] = true;
}
return false;
},
_numAscSort: function(a, b) {
return a - b;
},
_numDescSort: function(a, b) {
return b - a;
},
_sortAsc: function(x, y) {
if(x > y) {
return 1;
} else {
return -1;
}
},
_sortDesc: function(x, y) {
if(x > y) {
return -1;
} else {
return 1;
}
}
},
/关于数组操作js结束/
/关于日期操作js开始/
tmDate: {
/*转换日期*/
_transferDate: function(date) {
if(typeof date == "string") {
return new Date(date.replace(/-/ig, "/"));
} else {
return date;
}
},
/*格式化日期*/
_toString: function(date, pattern) {
var d = this._transferDate(date);
return d.format(pattern);
},
/*获取两个时间相减的时间*/
_Date: function(date1, date2) {
var dateTime = this._numMillSecond(date1, date2);
return new Date(dateTime).format("yyyy-MM-dd");
},
//间隔年份
_numYear: function(date1, date2) {
var times = this._numDay(date1, date2);
return Math.floor(times / 365);
},
//间隔月份
_numMonth: function(date1, date2) {
var times = this._numDay(date1, date2);
return Math.floor(times / 30);
},
//间隔天数
_numDay: function(date1, date2) {
var times = this._numSecond(date1, date2);
var hour = this._var().hour;
var mills = this._var().mills;
return Math.ceil(times / (mills * hour));
},
//间隔时
_numHour: function(date1, date2) {
return Math.floor(this._numMillSecond(date1, date2) / (1000 * 60 * 60));
},
//间隔分
_numMinute: function(date1, date2) {
return Math.floor(this._numMillSecond(date1, date2) / (1000 * 60));
},
//间隔秒数
_numSecond: function(date1, date2) {
return Math.floor(this._numMillSecond(date1, date2) / 1000);
},
//间隔毫秒
_numMillSecond: function(date1, date2) {
var stimes = this._getTime(this._transferDate(date1));
var etimes = this._getTime(this._transferDate(date2));
return etimes - stimes;
},
_var: function() {
return {
hour: 24,
second: 60,
mills: 3600,
format: "yyyy-MM-dd",
dateFormat: "yyyy-MM-dd HH:mm:ss"
};
},
/*某个日期加上多少毫秒*/
_plusMillisSeconds: function(date, millisSeconds) {
var dateTime = this._getTime(date);
var mintimes = millisSeconds;
var rdate = dateTime * 1 + mintimes * 1;
return this._format(new Date(rdate));
},
/*某个日期加上多少秒*/
_plusSeconds: function(date, seconds) {
var dateTime = this._getTime(date);
var mintimes = seconds * 1000;
var rdate = dateTime * 1 + mintimes * 1;
return this._format(new Date(rdate));
},
/*某个日期加上多少分钟*/
_plusMinutes: function(date, minutes) {
var dateTime = this._getTime(date);
var mintimes = minutes * 60 * 1000;
var rdate = dateTime * 1 + mintimes * 1;
return this._format(new Date(rdate));
},
/*某个日期加上小时数*/
_plusHours: function(date, hours) {
var dateTime = this._getTime(date);
var mintimes = hours * 60 * 60 * 1000;
var rdate = dateTime + mintimes;
return this._format(new Date(rdate));
},
/*某个日期加上天数*/
_plusDays: function(date, days) {
var dateTime = this._getTime(date);
var mintimes = days * 60 * 60 * 1000 * 24;
var rdate = dateTime * 1 + mintimes * 1;
return this._format(new Date(rdate));
},
/*某个日期加上多少个月,这里是按照一个月30天来计算天数的*/
_plusMonths: function(date, months) {
var dateTime = this._getTime(date);
var mintimes = months * 30 * 60 * 60 * 1000 * 24;
var rdate = dateTime + mintimes * 1;
return this._format(new Date(rdate));
},
/*某个日期加上多少个年,这里是按照一个月365天来计算天数的,如果loop为true则按闰年计算*/
_plusYears: function(date, years, isLoop) {
var dateTime = this._getTime(date);
var day = 365;
if(isLoop) day = 366;
var mintimes = years * day * 60 * 60 * 1000 * 24;
var rdate = dateTime + mintimes;
return this._format(new Date(rdate));
},
/*某个日期加上某个日期,这样的操作视乎没什么意义*/
_plusDate: function(date1, date2) {
var dateTime = this._getTime(date1);
var dateTime2 = this._getTime(date2);;
var rdate = dateTime + dateTime2;
return this._format(new Date(rdate));
},
/*某个日期减去多少毫秒秒*/
_minusMillisSeconds: function(date, millisSeconds) {
var dateTime = this._getTime(date);
var mintimes = millisSeconds * 1;
var rdate = dateTime - mintimes;
return this._format(new Date(rdate));
},
/*某个日期减去多少秒*/
_minusSeconds: function(date, seconds) {
var dateTime = this._getTime(date);
var mintimes = seconds * 1000;
var rdate = dateTime - mintimes;
return this._format(new Date(rdate));
},
/*某个日期减去多少分钟*/
_minusMinutes: function(date, minutes) {
var dateTime = this._getTime(date);
var mintimes = minutes * 60 * 1000;
var rdate = dateTime - mintimes;
return this._format(new Date(rdate));
},
/*某个日期减去小时数*/
_minusHours: function(date, hours) {
var dateTime = this._getTime(date);
var mintimes = hours * 60 * 60 * 1000;
var rdate = dateTime - mintimes;
return this._format(new Date(rdate));
},
/*某个日期减去天数*/
_minusDays: function(date, days) {
var dateTime = this._getTime(date);
var mintimes = days * 60 * 60 * 1000 * 24;
var rdate = dateTime - mintimes;
return this._format(new Date(rdate));
},
/*某个日期减去多少个月,这里是按照一个月30天来计算天数的*/
_minusMonths: function(date, months) {
var dateTime = this._getTime(date);
var mintimes = months * 30 * 60 * 60 * 1000 * 24;
var rdate = dateTime - mintimes;
return this._format(new Date(rdate));
},
/*某个日期减去多少个年,这里是按照一个月365天来计算天数的*/
_minusYears: function(date, years, isLoop) {
var dateTime = this._getTime(date);
var day = 365;
if(isLoop) day = 366;
var mintimes = years * day * 60 * 60 * 1000 * 24;
var rdate = dateTime - mintimes;
return this._format(new Date(rdate));
},
/*某个日期减去某个日期,这样的操作视乎没什么意义*/
_minusDate: function(date1, date2) {
var dateTime = this._getTime(date1);
var dateTime2 = this._getTime(date2);;
var rdate = dateTime - dateTime2;
return this._format(new Date(rdate));
},
/*获取一个月有多少天*/
_getMonthOfDay: function(date1) {
var currentMonth = this._getFirstDayOfMonth(date1);
var nextMonth = this._getNextDayOfMonth(date1);
return this._numDay(currentMonth, nextMonth);
},
/*获取一年又多少天*/
_getYearOfDay: function(date) {
var firstDayYear = this._getFirstDayOfYear(date);
var lastDayYear = this._getLastDayOfYear(date);
return Math.ceil(this._numDay(firstDayYear, lastDayYear));
},
/*某个日期是当年中的第几天*/
_getDayOfYear: function(date1) {
return Math.ceil(this._numDay(this._getFirstDayOfYear(date1), date1));
},
/*某个日期是在当月中的第几天*/
_getDayOfMonth: function(date1) {
return Math.ceil(this._numDay(this._getFirstDayOfMonth(date1), date1));
},
/*获取某个日期在这一年的第几周*/
_getDayOfYearWeek: function(date) {
var numdays = this._getDayOfYear(date);
return Math.ceil(numdays / 7);
},
/*某个日期是在当月中的星期几*/
_getDayOfWeek: function(date1) {
return this._getWeek(date1);
},
/*获取在当前日期中的时间*/
_getHourOfDay: function(date) {
return this._getHour(date);
},
_eq: function(date1, date2) {
var stime = this._getTime(this._transferDate(date1));
var etime = this._getTime(this._transferDate(date2));
return stime == etime ? true : false;
},
/*某个日期是否晚于某个日期*/
_after: function(date1, date2) {
var stime = this._getTime(this._transferDate(date1));
var etime = this._getTime(this._transferDate(date2));
return stime < etime ? true : false;
},
/*某个日期是否早于某个日期*/
_before: function(date1, date2) {
var stime = this._getTime(this._transferDate(date1));
var etime = this._getTime(this._transferDate(date2));
return stime > etime ? true : false;
},
/*获取某年的第一天*/
_getFirstDayOfYear: function(date) {
var year = this._getYear(date);
var dateString = year + "-01-01 00:00:00";
return dateString;
},
/*获取某年的最后一天*/
_getLastDayOfYear: function(date) {
var year = this._getYear(date);
var dateString = year + "-12-01 00:00:00";
var endDay = this._getMonthOfDay(dateString);
return year + "-12-" + endDay + " 23:59:59";
},
/*获取某月的第一天*/
_getFirstDayOfMonth: function(date) {
var year = this._getYear(date);
var month = this._getMonth(date);
var dateString = year + "-" + month + "-01 00:00:00";
return dateString;
},
/*获取某月最后一天*/
_getLastDayOfMonth: function(date) {
var endDay = this._getMonthOfDay(date);
var year = this._getYear(date);
var month = this._getMonth(date);
return year + "-" + month + "-" + endDay + " 23:59:59";
},
/*一天的开始时间*/
_getFirstOfDay: function(date) {
var year = this._getYear(date);
var month = this._getMonth(date);
var dates = this._getDay(date);
return year + "-" + month + "-" + dates + " 00:00:00";
},
/*一天的结束时间*/
_getLastOfDay: function(date) {
var year = this._getYear(date);
var month = this._getMonth(date);
var dates = this._getDay(date);
return year + "-" + month + "-" + dates + " 23:59:59";
},
/*获取下个月的第一天*/
_getNextDayOfMonth: function(date) {
var year = this._getYear(date);
var month = this._getMonth(date);
month = month * 1 + 1;
if(month > 12) {
year = year + 1;
month = month - 12;
}
month = month > 9 ? month : "0" + month;
var dateString = year + "-" + month + "-01 00:00:00";
return dateString;
},
_getFirstOfWeek: function(date1) {
var week = this._getWeek(date1);
var date = this._minusDays(date1, week);
var year = this._getYear(date);
var month = this._getMonth(date);
var dates = this._getDay(date);
return year + "-" + month + "-" + dates + " 00:00:00";
},
_getLastOfWeek: function(date1) {
var week = 6 - this._getWeek(date1);
var date = this._minusDays(date1, week);
var year = this._getYear(date);
var month = this._getMonth(date);
var dates = this._getDay(date);
return year + "-" + month + "-" + dates + " 23:59:59";
},
_getNow: function() {
return new Date();
},
_format: function(date) {
return this._getYear(date) + "-" + this._getMonth(date) + "-" + this._getDay(date) + " " + this._getHour(date) + ":" + this._getMinute(date) + ":" + this._getSecond(date);
},
_getDate: function() {
return this._getNow();
},
/*年*/
_getYear: function(date) {
return this._transferDate(date).getFullYear();
},
/*月*/
_getMonth: function(date) {
var month = this._transferDate(date).getMonth() + 1;
return month > 9 ? month : "0" + month;
},
/*日*/
_getDay: function(date) {
var day = this._transferDate(date).getDate();
return day > 9 ? day : "0" + day;
},
/*获取今天星期几,如果为0代表星期日*/
_getWeek: function(date) {
return this._transferDate(date).getDay();
},
/*时*/
_getHour: function(date) {
var hour = this._transferDate(date).getHours();
return hour > 9 ? hour : "0" + hour;
},
/*12小时制时*/
_getHour12: function(date) {
var hour = this._transferDate(date).getHours();
return hour % 12 == 0 ? 12 : hour % 12;
},
/*分*/
_getMinute: function(date) {
var minutes = this._transferDate(date).getMinutes();
return minutes > 9 ? minutes : "0" + minutes;
},
/*秒*/
_getSecond: function(date) {
var seconds = this._transferDate(date).getSeconds();
return seconds > 9 ? seconds : "0" + seconds;
},
/*毫秒*/
_getMillisecond: function(date) {
return this._transferDate(date).getMilliseconds();
},
/*获取今天在当年是第几季度*/
_getPeriod: function(date) {
var month = this._getMonth(date) * 1;
return Math.floor((month + 3) / 3);
},
/*星期*/
_nowWeekChinies: function(date) {
var nowWeek = this._getWeek(date);
var day = "";
switch(nowWeek) {
case 0:
day = "日";
break;
break;
case 1:
day = "一";
break;
break;
case 2:
day = "二";
break;
break;
case 3:
day = "三";
break;
break;
case 4:
day = "四";
break;
break;
case 5:
day = "五";
break;
break;
case 6:
day = "六";
break;
}
return day;
},
/*返回 1970 年 1 月 1 日至今的毫秒数。*/
_getTime: function(date) {
return this._transferDate(date).getTime();
},
/**
* 转换long值为日期字符串
* @param longtime 时间
* @param pattern 格式字符串,例如:yyyy-MM-dd hh:mm:ss
* @return 符合要求的日期字符串
*/
getFormatDateByLong: function(longTime, pattern) {
return this.getFormatDate(new Date(longTime), pattern);
},
/**
* 转换日期对象为日期字符串
* @param l long值
* @param pattern 格式字符串,例如:yyyy-MM-dd hh:mm:ss
* @return 符合要求的日期字符串
*/
getFormatDate: function(date, pattern) {
if(date == undefined) {
date = new Date();
}
if(pattern == undefined) {
pattern = "yyyy-MM-dd hh:mm:ss";
}
var o = {
"M+": date.getMonth() + 1,
"d+": date.getDate(),
"h+": date.getHours(),
"m+": date.getMinutes(),
"s+": date.getSeconds(),
"q+": Math.floor((date.getMonth() + 3) / 3),
"S": date.getMilliseconds()
};
if(/(y+)/.test(pattern)) {
pattern = pattern.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
}
for(var k in o) {
if(new RegExp("(" + k + ")").test(pattern)) {
pattern = pattern.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
}
}
return pattern;
},
/**
* 获取时间戳
* @param obj
* @returns {number|Number}
*/
timeStamp: function() {
return new Date().getTime();
},
/**
* 返回一个时间格式
* dateformat(new Data(),"y-M-d") "y-M-d" "y年M月d日" "y/M/d"
* dateformat(new Data(),"H-m-s") "H-m-s" "H时m分s秒" "H/m/s" "H:m:s"
* dateformat(new Data(),"y-M-d H:m:s") "y年M月d日 H时m分s秒" "y/M/d H/m/s" "y-M-d H:m:s"
* dateformat(new Date(),"MX")) 返回一天中的某个时刻 早晨 中午 下午 晚上 凌晨
* dateformat(new Date(),"yyyy") 获取年份
* dateformat(new Date(),"MM") 获取月份
* dateformat(new Date(),"dd") 获取日
* dateformat(new Date(),"HH") 获取小时
* dateformat(new Date(),"mm") 获取分
* dateformat(new Date(),"ss") 获取秒
* dateformat(new Date(),"WD") 获取星期
* dateformat(new Date(),"SMX") 获取几个月以前,几年以前
* @param obj
* @returns {number|Number}
* 调用方法:var str = utils.dateformat(new Data(),"y-M-d")
* @param {Object} data
* @param {Object} pattern
*/
dateformat: function(data, pattern) {
var month_day = data.getDate();
var week_day = data.getDay();
var year_month = data.getMonth() + 1;
var year_full = data.getFullYear();
var day_hour = data.getHours();
var hour_minute = data.getMinutes();
var minute_seconds = data.getSeconds();
var minute_Millisecond = data.getMilliseconds();
var time_Millisecond = data.getTime();
if(year_full < 10) {
year_full = "0" + year_full
};
if(month_day < 10) {
month_day = "0" + month_day
};
if(day_hour < 10) {
day_hour = "0" + day_hour
};
if(hour_minute < 10) {
hour_minute = "0" + hour_minute
};
if(minute_seconds < 10) {
minute_seconds = "0" + minute_seconds
};
if(pattern == "yyyy" && pattern.length == 4) {
return year_full;
};
if(pattern == "MM" && pattern.length == 2) {
return year_month;
};
if(pattern == "dd" && pattern.length == 2) {
return month_day;
};
if(pattern == "HH" && pattern.length == 2) {
return day_hour;
};
if(pattern == "mm" && pattern.length == 2) {
return hour_minute;
};
if(pattern == "ss" && pattern.length == 2) {
return minute_seconds;
};
if(pattern == "SMX" && pattern.length == 3) {
data.setFullYear(2015)
data.setMonth(11)
data.setMinutes(8)
var new_time = new Date();
var new_year_month = new_time.getMonth() + 1;
var new_day_hour_mnuite = new_time.getMinutes();
var new_year = new_time.getFullYear();
if((new_year - data.getFullYear()) >= 1 && (new_year - data.getFullYear() <= 2)) {
return "一年以前";
}
if((new_year - data.getFullYear()) >= 3 && (new_year - data.getFullYear() <= 5)) {
return "三年以前";
}
if((new_year - data.getFullYear()) >= 5 && (new_year - data.getFullYear() <= 8)) {
return "五年以前";
}
if((new_year - data.getFullYear()) >= 10) {
return "十年以前";
}
if(new_day_hour_mnuite - data.getMinutes() >= 3 && new_day_hour_mnuite - data.getMinutes() < 10) {
return "刚刚";
}
if(new_day_hour_mnuite - data.getMinutes() >= 10 && new_day_hour_mnuite - data.getMinutes() < 20) {
return "十分钟以前";
}
if(new_day_hour_mnuite - data.getMinutes() >= 380 && new_day_hour_mnuite - data.getMinutes() < 60) {
return "四十分钟以前";
}
if((new_year_month - data.getMonth() + 1) >= 1 && (new_year_month - data.getMonth() + 1) < 3) {
return "一个月以前";
}
if((new_year_month - data.getMonth() + 1) > 3 && (new_year_month - data.getMonth() + 1) < 6) {
return "三个月以前";
}
if((new_year_month - data.getMonth() + 1) > 6 && (new_year_month - data.getMonth() + 1) < 11) {
return "半年以前";
}
}
/*-------------------------------WD 返回星期----------start---------*/
if(pattern == "WD" && pattern.length == 2) {
var w_d;
switch(data.getUTCDay()) {
case 0:
w_d = "星期天";
break;
case 1:
w_d = "星期一";
break;
case 2:
w_d = "星期二";
break;
case 3:
w_d = "星期三";
break;
case 4:
w_d = "星期四";
break;
case 5:
w_d = "星期五";
break;
case 6:
w_d = "星期六";
break;
}
return w_d;
}
/*-------------------------------WD 返回星期----------end---------*/
/*-------------------------------MX 返回一天中的某个时刻--start-------------------*/
if(pattern == "MX" && pattern.length == 2) {
if(day_hour < 11 && day_hour > 6) {
return "早晨";
}
if(day_hour <= 14 && day_hour >= 11) {
return "中午";
}
if(day_hour > 14 && day_hour < 19) {
return "下午";
}
if(day_hour >= 19 && day_hour <= 23) {
return "晚上";
}
if(day_hour < 6 && day_hour >= 0) {
return "凌晨";
}
}
/*-------------------------------MX 返回一天中的某个时刻--end------------------*/
/*-------------------------------y-M-d H-m-s-------------start---------------------*/
//"y年M月d日 H时m分s秒"
var reg = /^(y{1,4}).?(M{1,2}).?(d{1,2})(([\ufe30-\uffA0]|[\u4e00-\u9fa5])+)?\s?(H{1,2}).?(m{1,2}).?(s{1,2})(([\ufe30-\uffA0]|[\u4e00-\u9fa5])+)?/;
var yMdHms_separator_reg = /([-+])|(\/+)|(([\ufe30-\uffA0]|[\u4e00-\u9fa5])+)(\s?)([-+])|(\/+)|([:+])|(([\ufe30-\uffA0]|[\u4e00-\u9fa5])+)/g;
var yMdHms_separator = pattern.match(yMdHms_separator_reg);
var yMdHms = pattern.match(reg);
if(reg.test(pattern)) {
if(pattern.length == 13) {
return year_full + yMdHms_separator[0] + year_month + yMdHms_separator[1] + month_day + yMdHms_separator[2] + " " + day_hour + yMdHms_separator[3] + hour_minute + yMdHms_separator[4] + minute_seconds + yMdHms_separator[5];
} else {
return year_full + yMdHms_separator[0] + year_month + yMdHms_separator[1] + month_day + " " + day_hour + yMdHms_separator[2] + hour_minute + yMdHms_separator[3] + minute_seconds
}
}
/*-------------------------------y-M-d H-m-s-------------end---------------------*/
/*-------------------------------y-M-d -------------start-----------------------*/
if(pattern.length >= 5) {
var yMd_separator = pattern.match(/([-+])|(\/+)|(([\ufe30-\uffA0]|[\u4e00-\u9fa5])+)/g);
var yMd = pattern.match(/^((y{1,4})-(M{1,2})-(d{1,2}))|((y{1,4})\/(M{1,2})\/(d{1,2}))|(([\ufe30-\uffA0]|[\u4e00-\u9fa5])+)/g);
if(utils.tmArray.arrContains(pattern, "M", false) || utils.tmArray.arrContains(pattern, "年", false)) {
if("y/M/d" === yMd[0] || "y-M-d" === yMd[0] || "y年M月d".indexOf("年") != -1 && "y年M月d".indexOf("月") != -1 && "y年M月d日".indexOf("日") != -1) {
if(yMd_separator.length == 3) {
return year_full + yMd_separator[0] + year_month + yMd_separator[1] + month_day + yMd_separator[2]
};
return year_full + yMd_separator[0] + year_month + yMd_separator[1] + month_day
};
};
/*-------------------------------y-M-d -------------end-----------------------*/
/*-------------------------------H-m-s -------------start-----------------------*/
var Hsm_separator = pattern.match(/([-+])|(\/+)|(:+)|(([\ufe30-\uffA0]|[\u4e00-\u9fa5])+)/g);
var Hsm = pattern.match(/^((H{1,4})-(m{1,2})-(s{1,2}))|((H{1,4})\/(m{1,2})\/(s{1,2}))|(([\ufe30-\uffA0]|[\u4e00-\u9fa5])+)|((H{1,4}):(m{1,2}):(s{1,2}))/g);
if(utils.tmArray.arrContains(pattern, "H", false) || utils.tmArray.arrContains(pattern, "分", false)) {
if("H/m/s" === Hsm[0] || "H-m-s" === Hsm[0] || "H时m分s秒".indexOf("时") != -1 && "H时m分s秒".indexOf("秒") != -1 && "H时m分s秒".indexOf("分") != -1) {
if(Hsm_separator.length == 3) {
return day_hour + Hsm_separator[0] + hour_minute + Hsm_separator[1] + minute_seconds + Hsm_separator[2]
};
return day_hour + Hsm_separator[0] + hour_minute + Hsm_separator[1] + minute_seconds;
};
};
};
}
},
/关于日期操作js结束/
/关于文件及字符操作js开始/
tmFile: {
/**
* 文件大小转换为MB GB KB格式
* @param {Object} size
*/
tm_countFileSize: function(size) {
var fsize = parseFloat(size, 2);
var fileSizeString;
if(fsize < 1024) {
fileSizeString = fsize.toFixed(2) + "B";
} else if(fsize < 1048576) {
fileSizeString = (fsize / 1024).toFixed(2) + "KB";
} else if(fsize < 1073741824) {
fileSizeString = (fsize / 1024 / 1024).toFixed(2) + "MB";
} else if(fsize < 1024 * 1024 * 1024) {
fileSizeString = (fsize / 1024 / 1024 / 1024).toFixed(2) + "GB";
} else {
fileSizeString = "0B";
}
return fileSizeString;
},
/**
* 获取文件的后缀名
* @param {Object} fileName
*/
tm_getExt: function(fileName) {
if(fileName.lastIndexOf(".") == -1)
return fileName;
var pos = fileName.lastIndexOf(".") + 1;
return fileName.substring(pos, fileName.length).toLowerCase();
},
/**
* 获取文件名称
* @param {Object} fileName
*/
tm_getFileName: function(fileName) {
var pos = fileName.lastIndexOf(".");
if(pos == -1) {
return fileName;
} else {
return fileName.substring(pos, fileName.length);
}
},
/**
* 生成一个随机数
*/
random: 0,
tm_Random: function() {
this.random++;
return new Date().getTime() + "" + random;
},
/**
* 过滤html代码(把<>转换)
* @param {Object} str
*/
filterTag: function(str) {
str = str.replace(/&/ig, "&");
str = str.replace(//ig, ">");
str = str.replace(" ", " ");
return str;
},
/**
* 过滤转换
* @param {Object} str
*/
filterScript: function(str) {
return str.replace(/(