以下一些在开发过程中封装的常用方法,供参考由于一些方法是很久之前写下来的,语法上比较旧,再使用的时候再进行修改//获取当前时刻的时间
// type = 1年月日,type=2时分秒,fommatter="-"表示年月日用-隔开,否则用"/"隔开 export function curTimeFun(type,fommatter) { const myDate = new Date(); const year = myDate.getFullYear()>9?myDate.getFullYear():‘0‘+myDate.getFullYear(); const month = (myDate.getMonth() + 1)>9?myDate.getMonth() + 1:‘0‘+(myDate.getMonth() + 1); const date = myDate.getDate()>9?myDate.getDate():‘0‘+myDate.getDate(); const h = myDate.getHours()>9?myDate.getHours():‘0‘+myDate.getHours(); //获取当前小时数(0-23) const m = myDate.getMinutes()>9?myDate.getMinutes():‘0‘+myDate.getMinutes(); //获取当前分钟数(0-59) const s = myDate.getSeconds()>9?myDate.getSeconds():‘0‘+myDate.getSeconds(); let nowTime = ""; if(type == ‘1‘){ if(fommatter == ‘-‘) { nowTime = year + ‘-‘ + month + "-" + date; } else { nowTime = year + ‘/‘ + month + "/" + date; } } else if(type == ‘2‘){ nowTime = h + ‘:‘ + m + ":" + s; } else { if(fommatter == ‘-‘) { nowTime = year + ‘-‘ + month + "-" + date + " " + h + ‘:‘ + m + ":" + s; } else { nowTime = year + ‘/‘ + month + "/" + date + " " + h + ‘:‘ + m + ":" + s; } } return nowTime; }
// 获取今天星期几
export function getWeekFun() { const weekDay = [‘星期日‘,‘星期一‘,‘星期二‘,‘星期三‘,‘星期四‘,‘星期五‘,‘星期六‘] let week = new Date().getDay(); return weekDay[week]; }
// 查询最近n年,n表示前多少年的意思// 例如查询近5年的时间,n=4,不包括今年的的前4年
export function getLastNYear(n) { const myDate = new Date; const curYear = myDate.getFullYear(); if(n ==‘‘ || n==undefined || n == null){ n =0; } let rstYear = curYear*1 - n*1; return rstYear; }
// 查询最近n月,n表示前多少月的意思
export function getLastNMonth(n) { const myDate = new Date; const curYear = myDate.getFullYear(); const curMonth = myDate.getMonth()+1; // 月份从0开始算起。需要加1 if(n ==‘‘ || n==undefined || n == null){n =0;} let rstYear = ‘‘; let rstMonth = ‘‘; if(n>curMonth){ //表示去到去年的月份,年份需要去到上一年 rstYear = curYear*1-1*1; rstMonth = 12-(n-curMonth)+1; } else { rstYear =curYear; rstMonth = curMonth -n; } rstMonth = (rstMonth)>9?rstMonth:‘0‘+(rstMonth); let rstYearMonth = rstYear + ‘-‘ + rstMonth; return rstYearMonth; }
// 获取最近n天的时间,n表示前多少天的意思。 // 例如查询近7天的时间,n=6,不包括当天的的前6天
export function getLastNDate(n,fommatter = ‘-‘) { const d = new Date(); var myDate=new Date(d.getTime()-86400000*n); // 获取前n天的日期 const year = myDate.getFullYear()>9?myDate.getFullYear():‘0‘+myDate.getFullYear(); const month = (myDate.getMonth() + 1)>9?myDate.getMonth() + 1:‘0‘+(myDate.getMonth() + 1); const date = myDate.getDate()>9?myDate.getDate():‘0‘+myDate.getDate();
let last7Date = year + fommatter + month + fommatter + date; return last7Date;
return last7Date; }
// 获取最近的n个小时,n表示前多少小时的意思。
export function getLastNHour(n,fommatter = ‘-‘) { const d = new Date(); var myDate=new Date(d.getTime()-86400000*n); // 获取前n天的日期 const year = myDate.getFullYear()>9?myDate.getFullYear():‘0‘+myDate.getFullYear(); const month = (myDate.getMonth() + 1)>9?myDate.getMonth() + 1:‘0‘+(myDate.getMonth() + 1); const date = myDate.getDate()>9?myDate.getDate():‘0‘+myDate.getDate(); const h = myDate.getHours()>9?myDate.getHours():‘0‘+myDate.getHours(); //获取当前小时数(0-23) const m = myDate.getMinutes()>9?myDate.getMinutes():‘0‘+myDate.getMinutes(); //获取当前分钟数(0-59) const s = myDate.getSeconds()>9?myDate.getSeconds():‘0‘+myDate.getSeconds(); let nowTime = year + fommatter + month + fommatter + date + " " + h + fommatter + m + fommatter + s; return nowTime; }
/*菜单树下拉,禁止选择当前项及其子项,以防止死循环*/
export function diGuiTreeEdit(item,compID) { // 编辑情况下,禁止选择当前节点及其子节点 let data = item; let treeAry = []; for (let i in data) { let v = data[i]; let node = {}; if (v === null || v === undefined) {} else { if (v.children && v.children.length > 0) { if (v.id == compID) { node.isDisabled = true; } else { node.isDisabled = false; } node.id = v.id; node.label = v.label; node.name = v.name; node.children = diGuiTreeEdit(v.children, compID); treeAry.push(node); } else { if (v.id == compID) { node.isDisabled = true; } else { node.isDisabled = false; } node.id = v.id; node.label = v.label; node.name = v.name; treeAry.push(node); } } } return treeAry; };
// 判断数据是否为空,为空的话返回指定的数值
export function isNullORFalse(checkVal,rstVal) { if(checkVal == ‘‘ || checkVal == null || checkVal == undefined || typeof(checkVal) == undefined){ if(rstVal == null || rstVal == ‘‘){ rstVal = ""; } return rstVal; } else { return checkVal; } }
// 全部替换
// str表示原字符串,s表示要被替换的内容,replaceStr用于替换的内容 export function repalceAll(str, s ,replaceStr = ‘‘) { var reg = new RegExp(s, "g")//g,表示全部替换 return str.replace(reg,replaceStr) }
//计算两个日期天数差的函数,通用,时间格式为yyyy-MM-dd格式
export function dateDiff(sDate1, sDate2) { //sDate1和sDate2是yyyy-MM-dd格式 let aDate, oDate1, oDate2, iDays aDate = sDate1.split("-") oDate1 = new Date(aDate[1] + ‘-‘ + aDate[2] + ‘-‘ + aDate[0]) //转换为yyyy-MM-dd格式 aDate = sDate2.split("-") oDate2 = new Date(aDate[1] + ‘-‘ + aDate[2] + ‘-‘ + aDate[0]) iDays = parseInt(Math.abs(oDate1 - oDate2) / 1000 / 60 / 60 / 24) //把相差的毫秒数转换为天数 return iDays; //返回相差天数 }
// 当数值过大是,超过9位数(亿)转为亿单位展示
export function rspHundredMill(val, unit = ‘HM‘) { if(unit == ‘HM‘) { if(val>100000000) { //大于1亿 return (val/100000000).toFixed(3)+‘亿‘ } else { return val } } }
// 根据id,以及id的值,遍历list对象,得出label
export function rstLabelFormID(list, id, label, idVal) { let rsp = ‘‘ for (let i = 0; i < list.length; i++) { if (list[i][id] === idVal) { rsp = list[i][label] break } } return rsp }
// 根据id,以及id的值,遍历list集合,得出对应的对象
export function rstListFormID(list, id, idVal) { let rsp = undefined for (let i = 0; i < list.length; i++) { if (list[i][id] === idVal) { rsp = list[i] break } } return rsp }
// 对象深拷贝
export function deepCopy(obj) { var result = Array.isArray(obj) ? [] : {} for (var key in obj) { if (obj.hasOwnProperty(key)) { if (typeof obj[key] === ‘object‘ && obj[key] !== null) { result[key] = deepCopy(obj[key]) } else { result[key] = obj[key] } } } return result }
// 拖动调整左右两侧div的宽度
// resizeName表示拖到的工具条的className // left表示左边的区域的className。leftWid表示左边的最小宽度 // midName表示右边区域的className。rightWid表示右边的最小宽度 // boxName表示父级元素的className export function dragControllerDiv(resizeName=‘resize‘, leftName = ‘left‘, midName = ‘mid‘, boxName = ‘box‘, leftWid = 250, rightWid = 800){ var resize = document.getElementsByClassName(resizeName); var left = document.getElementsByClassName(leftName); var mid = document.getElementsByClassName(midName); var box = document.getElementsByClassName(boxName); for (let i = 0; i < resize.length; i++) { // 鼠标按下事件 resize[i].onmousedown = function (e) { var startX = e.clientX; resize[i].left = resize[i].offsetLeft; // 鼠标拖动事件 document.onmousemove = function (e) { var endX = e.clientX; var moveLen = resize[i].left + (endX - startX); // (endx-startx)=移动的距离。resize[i].left+移动的距离=左边区域最后的宽度 var maxT = box[i].clientWidth - resize[i].offsetWidth; // 容器宽度 - 左边区域的宽度 = 右边区域的宽度 if (moveLen < leftWid) moveLen = leftWid; // 左边区域的最小宽度为150px if (moveLen > maxT - rightWid) moveLen = maxT - rightWid; //右边区域最小宽度为800px resize[i].style.left = moveLen; // 设置左侧区域的宽度 for (let j = 0; j < left.length; j++) { left[j].style.width = moveLen + ‘px‘; mid[j].style.width = (box[i].clientWidth - moveLen - 10) + ‘px‘; } } // 鼠标松开事件 document.onmouseup = function (evt) { document.onmousemove = null; document.onmouseup = null; resize[i].releaseCapture && resize[i].releaseCapture(); //当你不在需要继续获得鼠标消息就要应该调用ReleaseCapture()释放掉 } resize[i].setCapture && resize[i].setCapture(); //该函数在属于当前线程的指定窗口里设置鼠标捕获 return false; } } }
原文地址:https://www.cnblogs.com/luoxuemei/p/11646108.html
时间: 2024-10-07 10:40:01