import moment from 'moment' function formatTime(time) { if (typeof time !== 'number' || time < 0) { return time } var hour = parseInt(time / 3600) time = time % 3600 var minute = parseInt(time / 60) time = time % 60 var second = time return ([hour, minute, second]).map(function(n) { n = n.toString() return n[1] ? n : '0' + n }).join(':') } function formatLocation(longitude, latitude) { if (typeof longitude === 'string' && typeof latitude === 'string') { longitude = parseFloat(longitude) latitude = parseFloat(latitude) } longitude = longitude.toFixed(2) latitude = latitude.toFixed(2) return { longitude: longitude.toString().split('.'), latitude: latitude.toString().split('.') } } var dateUtils = { UNITS: { '年': 31557600000, '月': 2629800000, '天': 86400000, '小时': 3600000, '分钟': 60000, '秒': 1000 }, humanize: function(milliseconds) { var humanize = ''; for (var key in this.UNITS) { if (milliseconds >= this.UNITS[key]) { humanize = Math.floor(milliseconds / this.UNITS[key]) + key + '前'; break; } } return humanize || '刚刚'; }, format: function(dateStr) { var date = this.parse(dateStr) var diff = Date.now() - date.getTime(); if (diff < this.UNITS['天']) { return this.humanize(diff); } var _format = function(number) { return (number < 10 ? ('0' + number) : number); }; return date.getFullYear() + '/' + _format(date.getMonth() + 1) + '/' + _format(date.getDay()) + '-' + _format(date.getHours()) + ':' + _format(date.getMinutes()); }, parse: function(str) { //将"yyyy-mm-dd HH:MM:ss"格式的字符串,转化为一个Date对象 var a = str.split(/[^0-9]/); return new Date(a[0], a[1] - 1, a[2], a[3], a[4], a[5]); } }; /** *对Date的扩展,将 Date 转化为指定格式的String *月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符, *年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字) *例子: *(new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423 *(new Date()).Format("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18 */ Date.prototype.Format = function(fmt) { //author: meizz var o = { "M+": this.getMonth() + 1, //月份 "d+": this.getDate(), //日 "H+": this.getHours(), //小时 "m+": this.getMinutes(), //分 "s+": this.getSeconds(), //秒 "q+": Math.floor((this.getMonth() + 3) / 3), //季度 "S": this.getMilliseconds() //毫秒 }; if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length)); for (var k in o) if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (( "00" + o[ k]).substr(("" + o[k]).length))); return fmt; } Array.prototype.indexOf = function(val) { for (var i = 0; i < this.length; i++) { if (this[i] == val) { return i; } } return -1; } Array.prototype.remove = function(val) { var index = this.indexOf(val); if (index > -1) { this.splice(index, 1); } } String.prototype.Trim = function() { return this.replace(/(^\s*)|(\s*$)/g, ""); } String.prototype.LTrim = function() { return this.replace(/(^\s*)/g, ""); } String.prototype.RTrim = function() { return this.replace(/(\s*$)/g, ""); } function formatNumber(n) { n = n.toString() return n[1] ? n : '0' + n } function objectToUrlParams(obj) { var str = ""; for (var key in obj) { str += "&" + key + "=" + obj[key]; } return str.substr(1); } function getDate(type) { const date = new Date(); let year = date.getFullYear(); let month = date.getMonth() + 1; let day = date.getDate(); if (type === 'start') { year = year - 60; } else if (type === 'end') { year = year + 2; } month = month > 9 ? month : '0' + month; day = day > 9 ? day : '0' + day; return `${year}-${month}-${day}`; } function formatDate(date, type = 2) { if (type === 1) { return moment(date).format('HH:mm') } else if (type === 2) { return moment(date).format('YYYY-MM-DD HH:mm') } else if (type === 3) { return moment(date).format('YYYY.MM.DD') } else if (type === 4) { return moment(date).format('HH:mm:ss') } else if (type === 5) { return moment(date).format('YYYY-MM-DD HH:mm:ss') } else if (type === 6) { const result = moment(date).format('M月DD日 Ah:mm') return result.includes('PM') ? result.replace('PM', '下午') : result.replace('AM', '上午') } else if (type === 7) { return moment(date).format('MM-DD HH:mm') } else if (type === 8) { return moment(date).format('YYYY年MM月DD日') } else if(type === 9) { return moment(date).format('YYYY-MM-DD HH:mm:ss ddd') } else { return moment(date).locale().format('YYYY-MM-DD HH:mm:ss') } } /*获取当前时间*/ function getNowTime() { return Math.round(new Date().getTime() / 1000) } // 判断是否在签到时间内 function signBetweenTime(beginTime = undefined, endTime = undefined) { return new moment().isBetween(beginTime, endTime, null, '[]') } // 比较时间在此之前 function isBefore(time = undefined, cTime = undefined) { if(!cTime) { return new moment().isBefore(time) } return new moment(cTime).isBefore(time) } function isAfter(time = undefined, cTime = undefined) { if(!cTime) { return new moment().isAfter(time) } return new moment(cTime).isAfter(time) } function openDocument(urlPath) { // #ifdef APP uni.$u.toast('正在加载文件...') uni.downloadFile({ url: urlPath, success: function(res) { uni.$u.toast('文件加载成功') const filePath = res.tempFilePath uni.openDocument({ filePath, showMenu: true, success: function(res) { console.log('打开文档成功') } }) } }) // #endif // #ifndef APP uni.openDocument({ filePath: urlPath }) // #endif } // 金额小数点格式化, len是需要精确的小数点位数 function toFixedFun(data, len) { const number = Number(data); if (isNaN(number) || number >= Math.pow(10, 21)) { return number.toString(); } if (typeof(len) === 'undefined' || len === 0) { return (Math.round(number)).toString(); } let result = number.toString(); const numberArr = result.split('.'); if (numberArr.length < 2) { // 整数的情况 return padNum(result); } const intNum = numberArr[0]; // 整数部分 const deciNum = numberArr[1]; // 小数部分 const lastNum = deciNum.substr(len, 1); // 最后一个数字 if (deciNum.length === len) { // 需要截取的长度等于当前长度 return result; } if (deciNum.length < len) { // 需要截取的长度大于当前长度 1.3.toFixed(2) return padNum(result); } // 需要截取的长度小于当前长度,需要判断最后一位数字 result = `${intNum}.${deciNum.substr(0, len)}`; if (parseInt(lastNum, 10) >= 5) { // 最后一位数字大于5,要进位 const times = Math.pow(10, len); // 需要放大的倍数 let changedInt = Number(result.replace('.', '')); // 截取后转为整数 changedInt++; // 整数进位 changedInt /= times; // 整数转为小数,注:有可能还是整数 result = padNum(`${changedInt }`); } return result; // 对数字末尾加0 function padNum(num) { const dotPos = num.indexOf('.'); if (dotPos === -1) { // 整数的情况 num += '.'; for (let i = 0; i < len; i++) { num += '0'; } return num; } else { // 小数的情况 const need = len - (num.length - dotPos - 1); for (let j = 0; j < need; j++) { num += '0'; } return num; } } } // 格式化手机号码* function formatPhone(value) { if(!value) return '' if(value.toString().trim().length != 11) { return value } const phone = value.toString().trim().split('') phone.splice(3,4,'****') return phone.join('') } // 格式化身份证号* function formatIdCard(value) { if(!value) return '' const valueStr = value.toString().trim() if(valueStr.length != 18) { return value } const idcard = value.toString().trim().split('') idcard.splice(6,8,'********') return idcard.join('') } module.exports = { formatTime: formatTime, formatLocation: formatLocation, dateUtils: dateUtils, objectToUrlParams: objectToUrlParams, getDate: getDate, getNowTime: getNowTime, formatDate: formatDate, openDocument: openDocument, signBetweenTime: signBetweenTime, isBefore: isBefore, isAfter: isAfter, toFixedFun: toFixedFun, formatPhone: formatPhone, formatIdCard: formatIdCard }