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('.') } } //获取两日期之间日期列表函数 function getdiffdate(stime,etime){ //初始化日期列表,数组 var diffdate = new Array(); var i=0; //开始日期小于等于结束日期,并循环 while(stime<=etime){ diffdate[i] = stime; //获取开始日期时间戳 var stime_ts = new Date(stime).getTime(); //增加一天时间戳后的日期 var next_date = stime_ts + (24*60*60*1000); //拼接年月日,这里的月份会返回(0-11),所以要+1 var next_dates_y = new Date(next_date).getFullYear()+'-'; var next_dates_m = (new Date(next_date).getMonth()+1 < 10)?'0'+(new Date(next_date).getMonth()+1)+'-':(new Date(next_date).getMonth()+1)+'-'; var next_dates_d = (new Date(next_date).getDate() < 10)?'0'+new Date(next_date).getDate():new Date(next_date).getDate(); stime = next_dates_y+next_dates_m+next_dates_d; //增加数组key i++; } return diffdate; } /** * 计算两个日期之间的天数 * @param dateString1 开始日期 yyyy-MM-dd * @param dateString2 结束日期 yyyy-MM-dd * @returns {number} 如果日期相同 返回一天 开始日期大于结束日期,返回0 */ const getDaysBetween = (dateString1, dateString2) => { let startDate = Date.parse(dateString1); let endDate = Date.parse(dateString2); if (startDate > endDate) { return 0; } if (startDate == endDate) { return 1; } let days = (endDate - startDate) / (1 * 24 * 60 * 60 * 1000); return days; } 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]); } }; // 回显数据字典 const selectDictLabel = (datas, value) => { var actions = []; Object.keys(datas).some((key) => { if (datas[key].value == ('' + value)) { actions.push(datas[key].label); return true; } }) return actions.join(''); } /** * 乘法函数,用来得到精确的乘法结果 * @param {Object} arg1 * @param {Object} arg2 */ const accMul = (arg1, arg2) => { var m = 0; m += deal(arg1); m += deal(arg2); var r1 = Number(arg1.toString().replace('.', '')); var r2 = Number(arg2.toString().replace('.', '')); return (r1 * r2) / Math.pow(10, m); } /** * 加法函数,用来得到精确的加法结果 * @param {Object} arg1 * @param {Object} arg2 */ const accAdd = (arg1, arg2) => { var r1 = deal(arg1); var r2 = deal(arg2); var m = Math.pow(10, Math.max(r1, r2)); return (arg1 * m + arg2 * m) / m; } /** * 除法函数,用来得到精确的除法结果 * @param {Object} arg1 * @param {Object} arg2 */ const accDiv = (arg1, arg2) => { var t1 = deal(arg1); var t2 = deal(arg2); var r1 = Number(arg1.toString().replace(".", "")) var r2 = Number(arg2.toString().replace(".", "")) return (r1 / r2) * Math.pow(10, t2 - t1); } /** * 求小数点后的数据长度 */ const deal = (arg) => { var t = 0; try { t = arg.toString().split('.')[1].length; } catch (e) {} return t; } /** * 时间加上多少分 */ const dateTo = (date, minute) => { let time = new Date(date); time.setMinutes(time.getMinutes() + minute, time.getSeconds(), 0); return format(time); } /** * 判断 有效日期不能小于今天 */ const dateCompare = (beginDate, endDate) => { var beginArrayDate = beginDate.split('-'); var endArrayDate = endDate.split('-') if (parseInt(endArrayDate[0], 10) > parseInt(beginArrayDate[0], 10)) return true; if (parseInt(endArrayDate[0], 10) == parseInt(beginArrayDate[0], 10)) { if (parseInt(endArrayDate[1], 10) > parseInt(beginArrayDate[1], 10)) return true; else { if (parseInt(endArrayDate[1], 10) == parseInt(beginArrayDate[1], 10)) { if (parseInt(endArrayDate[2], 10) >= parseInt(beginArrayDate[2], 10)) return true; } } } return false; } /** * 获取日期 * day 获取日期 * time 获取时间 */ const getDate = (obj = 'day') => { const date = new Date(); let year = date.getFullYear(); let month = date.getMonth() + 1; let day = date.getDate(); let Hours = date.getHours(); let Minutes = date.getMinutes(); month = month > 9 ? month : '0' + month; day = day > 9 ? day : '0' + day; Hours = Hours > 9 ? Hours : '0' + Hours; Minutes = Minutes > 9 ? Minutes : '0' + Minutes; if (obj == 'day') { return `${year}-${month}-${day}`; } if (obj == 'time') { return `${year}-${month}-${day}` + ' ' + Hours + ':' + Minutes; } } /** * 格式化时间 */ const format = (obj) => { const date = new Date(obj); let year = date.getFullYear(); let month = date.getMonth() + 1; let day = date.getDate(); let Hours = date.getHours(); let Minutes = date.getMinutes(); month = month > 9 ? month : '0' + month; day = day > 9 ? day : '0' + day; Hours = Hours > 9 ? Hours : '0' + Hours; Minutes = Minutes > 9 ? Minutes : '0' + Minutes; return `${year}-${month}-${day}` + ' ' + Hours + ':' + Minutes; } function convertToChinaNum(num) { var arr1 = new Array('零', '一', '二', '三', '四', '五', '六', '七', '八', '九'); var arr2 = new Array('', '十', '百', '千', '万', '十', '百', '千', '亿', '十', '百', '千', '万', '十', '百', '千', '亿'); //可继续追加更高位转换值 if (!num || isNaN(num)) { return "零"; } var english = num.toString().split("") var result = ""; for (var i = 0; i < english.length; i++) { var des_i = english.length - 1 - i; //倒序排列设值 result = arr2[i] + result; var arr1_index = english[des_i]; result = arr1[arr1_index] + result; } //将【零千、零百】换成【零】 【十零】换成【十】 result = result.replace(/零(千|百|十)/g, '零').replace(/十零/g, '十'); //合并中间多个零为一个零 result = result.replace(/零+/g, '零'); //将【零亿】换成【亿】【零万】换成【万】 result = result.replace(/零亿/g, '亿').replace(/零万/g, '万'); //将【亿万】换成【亿】 result = result.replace(/亿万/g, '亿'); //移除末尾的零 result = result.replace(/零+$/, '') //将【零一十】换成【零十】 //result = result.replace(/零一十/g, '零十');//貌似正规读法是零一十 //将【一十】换成【十】 result = result.replace(/^一十/g, '十'); return result; } module.exports = { formatTime: formatTime, formatLocation: formatLocation, dateUtils: dateUtils, selectDictLabel: selectDictLabel, accMul: accMul, accAdd: accAdd, accDiv: accDiv, dateCompare: dateCompare, getDate: getDate, dateTo: dateTo, cn: convertToChinaNum, getDaysBetween: getDaysBetween, getdiffdate:getdiffdate }