const getToday = date => { const year = date.getFullYear() const month = date.getMonth() + 1 const day = date.getDate() const hour = date.getHours() const minute = date.getMinutes() const second = date.getSeconds() return [year, month, day].map(formatNumber).join('-'); } const formatNumber = n => { n = n.toString() //通过判断第二个数字是否存在在来判断是否大于9 return n[1] ? n : '0' + n } function checkTime(i) { if (i < 10) { i = "0" + i } return i; } // 中国标准时间格式化为yyyy-MM-dd HH:MM格式 function formatDate(value) { var date = new Date(value); return date.getFullYear() + '-' + checkTime((date.getMonth() + 1)) + '-' + checkTime(date.getDate()) + ' ' + checkTime((date.getHours())) + ':' + checkTime((date.getMinutes()));// + ':' + checkTime((date.getSeconds())) //最后显示yyyy-MM-dd HH:MM:ss } // 时间戳格式化为yyyy-MM-dd格式 function formatDateTime(value) { var date = new Date(parseInt(value)); return date.getFullYear() + '-' + checkTime((date.getMonth() + 1)) + '-' + checkTime(date.getDate()); //最后显示yyyy-MM-dd } // 获取当前周几转化为数字返回 function getWeek() { var date = new Date(); let week = date.getDay(); if (week == 0) week = 7; return week; } /** * 弹窗提示 */ function checkForm(warn) { wx.showToast({ title: warn, icon: 'none', duration: 2000, mask:true, }) return } /** * * @param {Number} AddDayCount 数值 0为今天,-1为昨天,1为明天 */ function GetDateStr(AddDayCount) { var dd = new Date(); dd.setDate(dd.getDate() + AddDayCount); //获取AddDayCount天后的日期 var y = dd.getFullYear(); var m = dd.getMonth() + 1; //获取当前月份的日期 m = m > 9 ? m : '0' + m; var d = dd.getDate(); d = d > 9 ? d : '0' + d; return y + "-" + m + "-" + d; } /** * 下拉框选择 * that [obj] this * setindex [string] 索引存放字段 * e [obj] event */ function getPickerIndex(that, index, e) { //弹窗提示 that.setData({ [index]: e.detail.value }) } /** * 找到字典对应下标 */ function getDicIndex(v_data, value, codeName) { var n = ''; for (var i = 0; i < v_data.length; i++) { if (v_data[i][codeName] == value) { n = i; } } return n; } /** * 找到字典对应text,用于详情显示 * @param {*} v_data 数组 * @param {*} value 数据库返回的值 * @param {*} codeName 用于匹配数组中的字段名 * @param {*} textCode 返回的字段名 */ function getDicName(v_data, value, codeName,textCode) { var n = ''; if (v_data){ for (var i = 0; i < v_data.length; i++) { if (v_data[i][codeName] == value) { n = v_data[i][textCode]; } } return n; } } /** * 选项卡的点击事件 */ function tabClickFun(e, that, currentTab) { if (that.data[currentTab] === e.target.dataset.current) { return false; } else { that.setData({ [currentTab]: e.target.dataset.current }) } } /** * 选项卡的滑动事件 */ function tabBindFun(e, that, currentTab) { that.setData({ [currentTab]: e.detail.current }); } /** * 字符串截取 * @string value 需要截取的字符串 * @Number start 截取起始位置 * @Number end 截取结束位置 */ function tosubStr(value, start, end) { if (value) { return value.slice(start, end); } else { return '' } } /** * 显示数值输入,最多只能输入2位小数 * @param {*} value 数值 */ function digitLength(value) { var digit; if (/^(\d?)+(\.\d{0,2})?$/.test(value)) { //正则验证,提现金额小数点后不能大于两位数字 digit = value; } else { digit = value.substring(0, value.length - 1); } return digit } module.exports = { getToday, formatDate, //中国标准时间格式化为yyyy-MM-dd格式 formatDateTime,//时间戳转为日期格式 checkTime, checkForm, tabClickFun, tabBindFun, GetDateStr, //获取日期(今天,昨天,明天等前后日期字符串) getPickerIndex, getWeek, // 获取当前周几数字 getDicIndex, //获取select对应下标 getDicName,//返回对应code的text tosubStr, digitLength, //输入框限制输入小数位数 }