timeago.js
842 Bytes
'use strict';
import TA from 'timeago.js';
import moment from 'moment';
export default function timeago(timestamp) {
let timeagoStr = TA().format(timestamp, 'zh_CN');
let isNSecondsAgo = timeagoStr.indexOf('秒');
if (timeagoStr === '刚刚' || isNSecondsAgo !== -1) {
return '刚刚';
}
if (timeagoStr === '1天前') {
return '昨天';
}
let isNDaysAgo = timeagoStr.indexOf('天');
let isNWeeksAgo = timeagoStr.indexOf('周');
let isNMonthsAgo = timeagoStr.indexOf('月');
let isNYearsAgo = timeagoStr.indexOf('年');
let isFutrueTime = timeagoStr.indexOf('后');
if (isNDaysAgo !== -1 || isNWeeksAgo !== -1 || isNMonthsAgo !== -1 || isNYearsAgo !== -1 || isFutrueTime !== -1) {
return moment(timestamp, 'x').format('MM.DD.YYYY');
}
return timeagoStr;
}