做前端开发的同学是不是经常封装一些常用的函数方法,比如,日期格式、对象转换等。话不多说,直接总结一些常用的封装函数直接放在utils中拿来即用!
//数组对象深拷贝
const deepCopy = function (source) { return JSON.parse(JSON.stringify(source)) }
//日期格式转换 filterTime(time) { const date = new Date(time) const Y = date.getFullYear() // 年 const M = date.getMonth() + 1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1; //月 const D = date.getDate() < 10 ? '0'+(date.getDate()) : date.getDate() //日 const HH = date.getHours() < 10 ? '0'+(date.getHours()) : date.getHours() //时 const MM = date.getMinutes() < 10 ? '0'+(date.getMinutes()) : date.getMinutes() //分 return `${Y}-${M}-${D} ${HH}:${MM}` },
//日期转为 刚刚 几分钟 ... function timeago(dateTimeStamp){ var minute = 1000 * 60; //把分,时,天,周,半个月,一个月用毫秒表示 var hour = minute * 60; var day = hour * 24; var week = day * 7; var halfamonth = day * 15; var month = day * 30; var now = new Date().getTime(); //获取当前时间毫秒 console.log(now) var diffValue = now - dateTimeStamp;//时间差 if(diffValue < 0){ return; } var minC = diffValue/minute; //计算时间差的分,时,天,周,月 var hourC = diffValue/hour; var dayC = diffValue/day; var weekC = diffValue/week; var monthC = diffValue/month; if(monthC >= 1 && monthC <= 3){ result = " " + parseInt(monthC) + "月前" }else if(weekC >= 1 && weekC <= 3){ result = " " + parseInt(weekC) + "周前" }else if(dayC >= 1 && dayC <= 6){ result = " " + parseInt(dayC) + "天前" }else if(hourC >= 1 && hourC <= 23){ result = " " + parseInt(hourC) + "小时前" }else if(minC >= 1 && minC <= 59){ result =" " + parseInt(minC) + "分钟前" }else if(diffValue >= 0 && diffValue <= minute){ result = "刚刚" }else { var datetime = new Date(); datetime.setTime(dateTimeStamp); var Nyear = datetime.getFullYear(); var Nmonth = datetime.getMonth() + 1 < 10 ? "0" + (datetime.getMonth() + 1) : datetime.getMonth() + 1; var Ndate = datetime.getDate() < 10 ? "0" + datetime.getDate() : datetime.getDate(); var Nhour = datetime.getHours() < 10 ? "0" + datetime.getHours() : datetime.getHours(); var Nminute = datetime.getMinutes() < 10 ? "0" + datetime.getMinutes() : datetime.getMinutes(); var Nsecond = datetime.getSeconds() < 10 ? "0" + datetime.getSeconds() : datetime.getSeconds(); result = Nyear + "-" + Nmonth + "-" + Ndate } return result; }
//原数据 dataOld: [{ id: '33', name: '小明', sex: "男"}, { id: '44', name: '小红', sex: "女"}] //接口需要的数据 dataNew: [{ value: '33', label: '小明', sex: "男"}, { value: '44', label: '小红', sex: "女"}] //方法 function TurnToMy(dataOld){ let dataNew = []; //新数组 dataOld.map(item => { let obj = { value: item.id, label: item.name, } dataNew.push(obj); }); return dataNew }
标签:web,封装,diffValue,js,var,&&,date,const,day From: https://www.cnblogs.com/agen-su/p/16798183.html