globalApproach.js
按需引入:
import {XXX} from "globalApproach.js"
全局引入:
import "globalApproach.js"
/** * 获取hash参数 */ export const getHashParameter =(name)=> { var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i'); var url = decodeURIComponent(window.location.hash).split('?')[1]; if(url){ var r = url.match(reg); } if(r) { return decodeURIComponent(r[2]); } return null; }
/** * 获取url参数 */ export const getQueryVariable = (variable)=> { var query = decodeURIComponent(window.location.search.substring(1)); var vars = query.split("&"); for (var i=0;i<vars.length;i++) { var pair = vars[i].split("="); if(pair[0] == variable){ return pair[1]; } } return false; } /** * 将date转为 格式化 yyyy-mm-dd / HH:ss:mm */ export function formatDate (date, format) { if (!date) return if (!format) format = 'yyyy-MM-dd' switch (typeof date) { case 'string': date = new Date(date.replace(/-/, '/')) break case 'number': date = new Date(date) break } if (!date instanceof Date) return var dict = { 'yyyy': date.getFullYear(), 'M': date.getMonth() + 1, 'd': date.getDate(), 'H': date.getHours(), 'm': date.getMinutes(), 's': date.getSeconds(), 'MM': ('' + (date.getMonth() + 101)).substr(1), 'dd': ('' + (date.getDate() + 100)).substr(1), 'HH': ('' + (date.getHours() + 100)).substr(1), 'mm': ('' + (date.getMinutes() + 100)).substr(1), 'ss': ('' + (date.getSeconds() + 100)).substr(1) } return format.replace(/(yyyy|MM?|dd?|HH?|ss?|mm?)/g, function () { return dict[arguments[0]] }) } /** * 获取两个日期之间的天数 */ export function getDaysNum(strDateStart,strDateEnd){ let strSeparator = "-"; //日期分隔符 let oDate1; let oDate2; let iDays; oDate1= strDateStart.split(strSeparator); oDate2= strDateEnd.split(strSeparator); let strDateS = new Date(oDate1[0], oDate1[1]-1, oDate1[2]); let strDateE = new Date(oDate2[0], oDate2[1]-1, oDate2[2]); iDays = parseInt(Math.abs(strDateS - strDateE ) / 1000 / 60 / 60 /24);//把相差的毫秒数转换为天数 return iDays ; } /** * 对象数组属性排序(与快排时间复杂度差不多) */ export function compareSort(key,arr,order){ return arr.sort((value1,value2)=>{ var val1 = value1[key]; var val2 = value2[key]; if(order == 'down'){ return val2-val1; } if(order == 'up'){ return val1-val2; } }) }