封装okutil工具类
parent
33efedd93c
commit
3d66299834
107
js/okutils.js
107
js/okutils.js
|
|
@ -1,20 +1,91 @@
|
||||||
Date.prototype.dateFormat = function (fmt) {
|
var dateUtil = {
|
||||||
var o = {
|
/**
|
||||||
"M+": this.getMonth() + 1,
|
* 格式化时间
|
||||||
"d+": this.getDate(),
|
* @param date
|
||||||
"h+": this.getHours(),
|
* @param fmt
|
||||||
"m+": this.getMinutes(),
|
* @returns {*}
|
||||||
"s+": this.getSeconds(),
|
*/
|
||||||
"q+": Math.floor((this.getMonth() + 3) / 3),
|
dateFormat: function (date, fmt) {
|
||||||
"S": this.getMilliseconds()
|
var o = {
|
||||||
};
|
"M+": date.getMonth() + 1,
|
||||||
if (/(y+)/.test(fmt)) {
|
"d+": date.getDate(),
|
||||||
fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
|
"h+": date.getHours(),
|
||||||
|
"m+": date.getMinutes(),
|
||||||
|
"s+": date.getSeconds(),
|
||||||
|
"q+": Math.floor((date.getMonth() + 3) / 3),
|
||||||
|
"S": date.getMilliseconds()
|
||||||
|
};
|
||||||
|
if (/(y+)/.test(fmt))
|
||||||
|
fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
|
||||||
|
for (var k in o)
|
||||||
|
if (new RegExp("(" + k + ")").test(fmt))
|
||||||
|
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
|
||||||
|
return fmt;
|
||||||
}
|
}
|
||||||
for (var k in o) {
|
|
||||||
if (new RegExp("(" + k + ")").test(fmt)) {
|
|
||||||
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return fmt;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
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}
|
||||||
|
*/
|
||||||
|
isChinese: function (str) {
|
||||||
|
var reg = /^([u4E00-u9FA5]|[uFE30-uFFA0])*$/;
|
||||||
|
if (reg.test(str)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
|
||||||
12
okutils.html
12
okutils.html
|
|
@ -8,11 +8,13 @@
|
||||||
<body>
|
<body>
|
||||||
|
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
var now = new Date();
|
console.log("当前时间格式化:", dateUtil.dateFormat(new Date(), "yyyy-MM-dd hh:mm:ss"))
|
||||||
var formatNow1 = now.dateFormat("yyyy-MM-dd hh:mm:ss");
|
|
||||||
var formatNow2 = now.dateFormat("yyyy-MM-dd hh:mm");
|
console.log("判断字符串是否为空:", strUtil.isEmpty(""));
|
||||||
console.log(formatNow1);
|
console.log("判断两个字符串是否相同:", strUtil.isEquals("abc", "abc"));
|
||||||
console.log(formatNow2);
|
console.log("忽略大小写判断字符串是否相同:", strUtil.isEqualsIgnoreCase("abc", "Abc"));
|
||||||
|
console.log("判断是否是数字:", strUtil.isNum("123"));
|
||||||
|
console.log("判断是否是中文:", strUtil.isChinese("xxx你好"));
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue