Array
Date
时间戳 转 年-月-日 时:分
1 2 3 4 5 6 7 8 9 10 11
| let YMD = (now) => { if(now == null) return null; now = Number(now); var date = new Date(now); Y = date.getFullYear(); M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1); D = date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate(); H = date.getHours() < 10 ? '0' + (date.getHours()): date.getHours() ; F = date.getMinutes() < 10 ? '0' + (date.getMinutes()) : date.getMinutes(); return Y + '-' + M + '-' + D + '' + H + ':' + F; }
|
日期 年/月/日 时:分 转成 时间戳
1 2 3 4 5 6
| let unix = (args) => { var arr = args.split(" "); var time = (new Date(arr)).getTime(); console.log(time); // 1496909520000 单位:ms return time; }
|
根据时间戳判断不同的时间段显示今天,昨天,前天,具体日期
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| let format = (time) => { const oneDay = 24*60*60*1000; // 一天的毫秒数 var today = new Date(); var param = new Date(time); var d = Math.floor(today.getTime()/oneDay) - Math.floor(time/oneDay); if (d == 0) { // 今天 return '今天'; } else if (d == 1) { // 昨天 return '昨天'; } else if (d == 2) { // 前天 return '前天'; } else { // 显示具体日期 if (today.getFullYear() == param.getFullYear()) { // 不显示年 return param.getMonth()+1 + '-' + param.getDate(); } else { // 显示年 return param.getFullYear() + '-' + (param.getMonth()+1) + '-' + param.getDate(); } } }
|
Function
Math
Number
百分数转换成小数
1 2 3 4 5
| let percentTodecimal = (percent) = { var decimal = (String(percent).replace(/%/, ""))/100 ; //保留两位 return decimal.toFixed(2); }
|
数字转换成百分数
1 2 3 4 5
| let toPercent = (point) => { var str=Number(point*100).toFixed(1); str+="%"; return str; }
|
数字加单位
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| /** * 数字转整数 如 100000 转为10万 * @param {需要转化的数} num * @param {需要保留的小数位数} point */
let browseNumber = (num, point) => { let numStr = num.toString() // 万以内直接返回 if (numStr.length < 5) { return numStr; } //大于8位数是亿 else if (numStr.length > 8) { let decimal = numStr.substring(numStr.length - 8, numStr.length - 8 + parseInt(point)); return parseFloat(parseInt(num / 100000000) + '.' + decimal) + '亿'; } //大于4位数是万 else if (numStr.length > 4 && numStr.length <= 5) { let decimal = numStr.substring(numStr.length - 4, numStr.length - 4 + parseInt(point)) return parseFloat(parseInt(num / 10000) + '.' + decimal) + '万'; } //大于5位数是十万+ else if (numStr.length > 5) { let decimal = numStr.substring(numStr.length - 4, numStr.length - 4 + parseInt(point)) return parseFloat(parseInt(10)) + '万+'; } };
|
Object
判断对象的数据类型
使用 Object.prototype.toString 配合闭包,通过传入不同的判断类型来返回不同的判断函数(注意传入 type 参数时首字母大写)
不推荐将这个函数用来检测可能会产生包装类型的基本数据类型上,因为 call 会将第一个参数进行装箱操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| function dataType(){ // 预置目前已知的数据类型 var is ={ types : [ "String","Boolean", "Number", "Array", "Object", "Date", "RegExp","Window", "HTMLDocument"] }; for(var i = 0, c; c = is.types[ i++ ]; ){ is[c] = (function(type){ return function(obj){ return Object.prototype.toString.call(obj) == "[object " + type + "]"; } })(c); } // 利用return返回值,提供给外面调用 return is ; }
var is = dataType(); //检测是否是字符串对象 console.log(is.String('11')); // true
|
ES6的写法(推荐)
1 2 3 4
| const isType = type => target => '[object ${type}]' === Object.prototype.toString.call(target); const isArray = isType('Array'); console.log(isArray([])); > true
|
检测对象(不仅指Object)是否存在并且检测对象是否为空
方式一:jq $.isEmptyObject(obj); 空ture 非空false
1 2 3 4 5 6 7 8 9 10 11 12
| function isEmptyObject_1(obj){ if (typeof obj === "undefined") { console.log("该对象不存在"); }else{ if($.isEmptyObject(obj)==true){ console.log("空对象"); }else{ console.log("非空对象"); } } }
|
方式二:利用JSON.stringify(obj)方法检测
1 2 3 4 5 6 7 8 9 10 11
| function isEmptyObject_2(obj){ if (typeof obj === "undefined") { console.log("该对象不存在"); }else{ if(JSON.stringify(obj) == "{}"){ console.log("空对象"); }else{ console.log("非空对象"); } } }
|
String
indexOf查找字符串中是否有某个特殊字符
1 2 3 4
| // indexOf的用法: 含有这个字符返回索引位置,没有返回-1 let indexOf = (str,character) => { return str.indexOf(character); }
|
解析url的参数组装成对象返回
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| let parseQueryString = (url) => { let reg_url = /^[^\?]+\?([\w\W]+)$/, reg_para = /([^&=]+)=([\w\W]*?)(&|$|#)/g, arr_url = reg_url.exec(url), ret = {}
if (arr_url && arr_url[1]) { var str_para = arr_url[1], result; while ((result = reg_para.exec(str_para)) != null) { ret[result[1]] = result[2]; } } return ret };
|
其他
根据姓氏选取数组中预置的背景颜色
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| let getColor = (name) => { var color= ['#eead10','#f99a2b','#f38134','#6495ed','#3ab1aa','#0abfb5','#06aae1','#00bfff','#96bc53','#00ced1','#89a8e0']; var newName = encodeURI(name).replace(/%/g, ""); var lastName, hexadecimal, tenBinary; //长度大于等于6位,取后六位 if(newName.length >= 6) { lastName = newName.substr(0,6); hexadecimal = parseInt(lastName,16); //能转成数字 if(hexadecimal) { tenBinary = hexadecimal%10; return color[tenBinary]; } else { //不能转数字 return color[10]; } } else { return color[10] } }
|
表单获取焦点时placeholder的提示语消失,提升用户体验。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| let placeholder = () => { $(document).on('focus','textarea',function(){ $(this).attr('placeholder',''); if(!$.trim(this.value)){ this.value = ''; } }).on('blur','textarea',function(){ var salf = this; $(salf).attr('placeholder','请输入说明文字'); if(!$.trim(salf.value)){ this.value = ''; setTimeout(function(){salf.value = '';},100); } }); }
|