海南旅游项目 前端仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

64 lines
1.5 KiB

/*
全局共享实用方法 shared.js
*/
// 判断对错/是否显示,万能校验
export function isRight(obj) {
if (isValueType(obj) === 'string') {
obj = obj.trim();
if (obj === 'null' || obj === 'undefined') {
return false;
}
} else if (isValueType(obj) === 'number' && (isValueType(obj) === "number" && !isNaN(obj)) && obj !== 0) {
return true;
} else if (isValueType(obj) === 'boolean') {
return obj
}
for (var key in obj) {
return true;
}
return false;
}
// 判断一个值所属的类型,返回一个字符串
export function isValueType(value) {
let str = Object.prototype.toString.call(value);
return str.match(/\[object (.*?)\]/)[1].toLowerCase();
}
export const timer = (value, fmt) => {
if(!value) return;
let newTime = new Date(value)
if(!fmt){
fmt = 'yyyy-MM-dd hh:mm';
}
if(/(y+)/.test(fmt)) {
fmt = fmt.replace(RegExp.$1, (newTime.getFullYear() + '').substr(4 - RegExp.$1.length));
}
let o = {
'M+': newTime.getMonth() + 1,
'd+': newTime.getDate(),
'h+': newTime.getHours(),
'm+': newTime.getMinutes(),
's+': newTime.getSeconds()
};
function padLeftZero(str) {
return ('00'+str).substr(str.length);
}
// 遍历这个对象
for (let k in o) {
if (new RegExp(`(${k})`).test(fmt)) {
// console.log(`${k}`)
let str = o[k] + '';
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str));
}
}
return fmt;
}
// 位数不足2,前面补0
var cover = function(par) {
par = par.toString()[1] ? par : "0" + par;
return par;
};