123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- export function formatDate(timestamp) {
-
- let arrTimestamp = (timestamp + '').split('');
- for (let start = 0; start < 13; start++) {
- if (!arrTimestamp[start]) {
- arrTimestamp[start] = '0';
- }
- }
- timestamp = arrTimestamp.join('') * 1;
- let minute = 1000 * 60;
- let hour = minute * 60;
- let day = hour * 24;
- let month = day * 30;
- let now = new Date().getTime();
- let diffValue = now - timestamp;
-
- if (diffValue < 0) {
- return '不久前';
- }
-
- let monthC = diffValue / month;
- let weekC = diffValue / (7 * day);
- let dayC = diffValue / day;
- let hourC = diffValue / hour;
- let minC = diffValue / minute;
-
- let zero = function (value) {
- if (value < 10) {
- return '0' + value;
- }
- return value;
- };
-
- if (monthC > 4) {
-
- return (function () {
- let date = new Date(timestamp);
- return date.getFullYear() + '年' + zero(date.getMonth() + 1) + '月' + zero(date.getDate()) + '日';
- })();
- } else if (monthC >= 1) {
- return parseInt(monthC) + '月前';
- } else if (weekC >= 1) {
- return parseInt(weekC) + '周前';
- } else if (dayC >= 1) {
- return parseInt(dayC) + '天前';
- } else if (hourC >= 1) {
- return parseInt(hourC) + '小时前';
- } else if (minC >= 1) {
- return parseInt(minC) + '分钟前';
- }
- return '刚刚';
- }
|