UNPKG

web-utils-super

Version:

前端函数库

24 lines (22 loc) 773 B
/** * @desc 格式化startTime距现在的已过时间 * @param {Date | String} startTime * @return {String} */ function formatPassTime(startTime) { let currentTime = Date.parse(new Date()) if ((typeof startTime) === 'string') startTime = new Date(startTime) let time = currentTime - startTime let day = parseInt(time / (1000 * 60 * 60 * 24)) let hour = parseInt(time / (1000 * 60 * 60)) let min = parseInt(time / (1000 * 60)) let month = parseInt(day / 30) let year = parseInt(month / 12) if (year) return year + '年前' if (month) return month + '个月前' if (day) return day + '天前' if (hour) return hour + '小时前' if (min) return min + '分钟前' else return '刚刚' } module.exports = formatPassTime