1.当前星期几
* @params 无
* @returns String
export function getWeekDate() {
var now = new Date();
var day = now.getDay();
var weeks = new Array("周日", "周一", "周二", "周三", "周四", "周五", "周六");
var week = weeks[day];
return week;
}
2.当前时间
* @param { date } // new Date() 2022-12-20T02:42:56.883Z
* @param { str } // 日期格式 yyyy-MM-dd hh:mm:ss
* @returns { String } // 2022-12-20 10:42:18
export function formatDate(date, str) {
date = new Date(date);
str = str ? str : "yyyy/MM/dd hh:mm:ss";
if (/(y+)/.test(str)) {
str = str.replace(RegExp.$1, String(date.getFullYear()).substr(4 - RegExp.$1.length));
}
let o = {
"M+": date.getMonth() + 1,
"d+": date.getDate(),
"h+": date.getHours(),
"m+": date.getMinutes(),
"s+": date.getSeconds(),
};
for (let k in o) {
if (new RegExp(`(${k})`).test(str)) {
let stry = String(o[k]);
str = str.replace(RegExp.$1, RegExp.$1.length === 1 ? stry : ("00" + stry).substr(stry.length));
}
}
return str;
}
3.去重数组
* @param { 去重数组 } Array
* @param { 去重依赖的属性 } key
* @returns Array 去重后的数组
export function uniqueArray(arr = [], key) {
const keyValues = new Set();
let val;
return arr.filter((obj) => {
val = obj[key];
if (keyValues.has(val)) {
return false;
}
keyValues.add(val);
return true;
});
}
4.复杂List排序
* @param 需要依赖sort() // 例如 ——> const res = arr.sort(sortBy("id", false));
* @param { 排序依赖的属性 } attr
* @param { true为正序,false为倒叙 } rev
* @returns
export function sortBy(attr, rev) {
if (rev == undefined) {
rev = 1;
} else {
rev = rev ? 1 : -1;
}
return function (a, b) {
a = Number(a[attr]);
b = Number(b[attr]);
if (a < b) {
return rev * -1;
}
if (a > b) {
return rev * 1;
}
return 0;
};
}
5.检查数据类型
* @param { 检查的对象 } obj
* @returns
export function dataType(obj) {
var toString = Object.prototype.toString;
var map = {
"[object Boolean]": "boolean",
"[object Number]": "number",
"[object String]": "string",
"[object Function]": "function",
"[object Array]": "array",
"[object Date]": "date",
"[object RegExp]": "regExp",
"[object Undefined]": "undefined",
"[object Null]": "null",
"[object Object]": "object",
};
return map[toString.call(obj)];
}
标签:return,object,rev,param,str,date,uitls
From: https://www.cnblogs.com/songkomei/p/16993755.html