小程序时间戳转换日期
作者:互联网
/* * 时间戳转换标准时间 * author Joy Sang * */ var regYear = getRegExp("(y+)", "i"); var dateFormat = function (timestamp, format) { if (!format) { format = "yyyy-MM-dd hh:mm:ss"; } timestamp = parseInt(timestamp) * 1000; var realDate = getDate(timestamp); function timeFormat(num) { return num < 10 ? "0" + num : num; } var date = [ ["M+", timeFormat(realDate.getMonth() + 1)], ["d+", timeFormat(realDate.getDate())], ["h+", timeFormat(realDate.getHours())], ["m+", timeFormat(realDate.getMinutes())], ["s+", timeFormat(realDate.getSeconds())], ["q+", Math.floor((realDate.getMonth() + 3) / 3)], ["S+", realDate.getMilliseconds()] ]; var reg1 = regYear.exec(format); if (reg1) { format = format.replace( reg1[1], (realDate.getFullYear() + "").substring(4 - reg1[1].length) ); } for (var i = 0; i < date.length; i++) { var k = date[i][0], v = date[i][1]; var reg2 = getRegExp("(" + k + ")").exec(format); if (reg2) { format = format.replace( reg2[1], reg2[1].length === 1 ? v : ("00" + v).substring(("" + v).length) ); } } return format; }; var getDateDiff = function (dateTime, now) { var dateTimeStamp = dateTime * 1000; var result = ""; var minute = 1000 * 60; var hour = minute * 60; var day = hour * 24; var month = day * 30; var year = day * 365; var diffValue = now - dateTimeStamp; if (diffValue < 0) { return; } var monthEnd = diffValue / month; var weekEnd = diffValue / (7 * day); var dayEnd = diffValue / day; var hourEnd = diffValue / hour; var minEnd = diffValue / minute; var yearEnd = diffValue / year; if (yearEnd >= 1) { result = dateFormat(dateTime, 'yyyy-MM-dd hh:mm:ss'); } else if (monthEnd >= 1) { result = dateFormat(dateTime, 'yyyy-MM-dd hh:mm:ss'); } else if (weekEnd >= 1) { result = dateFormat(dateTime, 'yyyy-MM-dd hh:mm:ss'); } else if (dayEnd >= 1) { result = "" + parseInt(dayEnd) + "天前"; } else if (hourEnd >= 1) { result = "" + parseInt(hourEnd) + "小时前"; } else if (minEnd >= 1) { result = "" + parseInt(minEnd) + "分钟前"; } else { result = "刚刚"; } return result; }; var getDateDiff2 = function (dateTime) { var now = getDate().getTime(); var dateTimeStamp = dateTime * 1000; var result = ""; var minute = 1000 * 60; var hour = minute * 60; var day = hour * 24; var month = day * 30; var year = day * 365; var diffValue = now - dateTimeStamp; if (diffValue < 0) { return; } var monthEnd = diffValue / month; var weekEnd = diffValue / (7 * day); var dayEnd = diffValue / day; var hourEnd = diffValue / hour; var minEnd = diffValue / minute; var yearEnd = diffValue / year; if (yearEnd >= 1) { result = dateFormat(dateTime, 'MM-dd '); } else if (monthEnd >= 1) { result = dateFormat(dateTime, 'MM-dd'); } else if (weekEnd >= 1) { result = dateFormat(dateTime, 'MM-dd'); } else if (dayEnd >= 1 && dayEnd < 2) { // result = "" + parseInt(dayEnd) + "天前"; result = "昨天" } else if (dayEnd >= 2) { result = dateFormat(dateTime, 'MM-dd'); } else if (hourEnd >= 1) { result = "" + parseInt(hourEnd) + "小时前"; } else if (minEnd >= 1) { result = "" + parseInt(minEnd) + "分钟前"; } else { result = "刚刚"; } return result; }; module.exports = { dateFormat: dateFormat, getDateDiff: getDateDiff, getDateDiff2: getDateDiff2 };
复制上面代码新建timeUtil.wxs
<wxs src="../../utils/timeUtil.wxs" module="tool"></wxs>
在需要使用的页面,引入timeUtil.wxs;
最后进行时间戳转换
标签:转换,format,程序,diffValue,else,日期,result,var,dateTime 来源: https://www.cnblogs.com/yutianA/p/14512777.html