| import timeFormat from '../../libs/function/timeFormat.js'; |
| |
| /** |
| * 时间戳转为多久之前 |
| * @param String timestamp 时间戳 |
| * @param String | Boolean format 如果为时间格式字符串,超出一定时间范围,返回固定的时间格式; |
| * 如果为布尔值false,无论什么时间,都返回多久以前的格式 |
| */ |
| function timeFrom(timestamp = null, format = 'yyyy-mm-dd') { |
| if (timestamp == null) timestamp = Number(new Date()); |
| timestamp = parseInt(timestamp); |
| // 判断用户输入的时间戳是秒还是毫秒,一般前端js获取的时间戳是毫秒(13位),后端传过来的为秒(10位) |
| if (timestamp.toString().length == 10) timestamp *= 1000; |
| var timer = (new Date()).getTime() - timestamp; |
| timer = parseInt(timer / 1000); |
| // 如果小于5分钟,则返回"刚刚",其他以此类推 |
| let tips = ''; |
| switch (true) { |
| case timer < 300: |
| tips = '刚刚'; |
| break; |
| case timer >= 300 && timer < 3600: |
| tips = parseInt(timer / 60) + '分钟前'; |
| break; |
| case timer >= 3600 && timer < 86400: |
| tips = parseInt(timer / 3600) + '小时前'; |
| break; |
| case timer >= 86400 && timer < 2592000: |
| tips = parseInt(timer / 86400) + '天前'; |
| break; |
| default: |
| // 如果format为false,则无论什么时间戳,都显示xx之前 |
| if(format === false) { |
| if(timer >= 2592000 && timer < 365 * 86400) { |
| tips = parseInt(timer / (86400 * 30)) + '个月前'; |
| } else { |
| tips = parseInt(timer / (86400 * 365)) + '年前'; |
| } |
| } else { |
| tips = timeFormat(timestamp, format); |
| } |
| } |
| return tips; |
| } |
| |
| export default timeFrom; |