Commit d2041471138f6850349f4cd0ae9cf56d8d0bf188

Authored by 梁灏
1 parent b2391629

update Time

Showing 2 changed files with 15 additions and 12 deletions   Show diff stats
examples/routers/time.vue
... ... @@ -4,16 +4,16 @@
4 4 <br>
5 5 <Time :time="time2" />
6 6 <br>
7   - <Time :time="time3" :interval="60" />
  7 + <Time :time="time3" :interval="1" />
8 8 </div>
9 9 </template>
10 10 <script>
11 11 export default {
12 12 data () {
13 13 return {
14   - time1: (new Date()).getTime() - 60 * 1 * 1000,
  14 + time1: (new Date()).getTime() - 60 * 59 * 1000,
15 15 time2: (new Date()).getTime() - 86400 * 3 * 1000,
16   - time3: new Date()
  16 + time3: (new Date()).getTime() - 1 * 1000
17 17 }
18 18 }
19 19 }
... ...
src/components/time/time.js
... ... @@ -51,11 +51,13 @@ export const getRelativeTime = timeStamp =&gt; {
51 51 // 判断当前传入的时间戳是秒格式还是毫秒
52 52 const IS_MILLISECOND = true;
53 53 // 如果是毫秒格式则转为秒格式
54   - if (IS_MILLISECOND) Math.floor(timeStamp /= 1000);
  54 + // if (IS_MILLISECOND) Math.floor(timeStamp /= 1000);
55 55 // 传入的时间戳可以是数值或字符串类型,这里统一转为数值类型
56   - timeStamp = Number(timeStamp);
  56 + // timeStamp = Number(timeStamp);
57 57 // 获取当前时间时间戳
58   - const currentTime = Math.floor(Date.parse(new Date()) / 1000);
  58 + // const currentTime = Math.floor(Date.parse(new Date()) / 1000);
  59 + const currentTime = (new Date()).getTime();
  60 +
59 61 // 判断传入时间戳是否早于当前时间戳
60 62 const IS_EARLY = isEarly(timeStamp, currentTime);
61 63 // 获取两个时间戳差值
... ... @@ -64,17 +66,18 @@ export const getRelativeTime = timeStamp =&gt; {
64 66 if (!IS_EARLY) diff = -diff;
65 67 let resStr = '';
66 68 const dirStr = IS_EARLY ? '前' : '后';
  69 +
  70 + if (diff < 1000) resStr = '刚刚';
67 71 // 少于等于59秒
68   - if (diff < 1) resStr = '刚刚';
69   - else if (diff <= 59) resStr = parseInt(diff) + '秒' + dirStr;
  72 + else if (diff < 60000) resStr = parseInt(diff / 1000) + '秒' + dirStr;
70 73 // 多于59秒,少于等于59分钟59秒
71   - else if (diff > 59 && diff <= 3599) resStr = Math.ceil(diff / 60) + '分钟' + dirStr;
  74 + else if (diff >= 60000 && diff < 3600000) resStr = Math.floor(diff / 60000) + '分钟' + dirStr;
72 75 // 多于59分钟59秒,少于等于23小时59分钟59秒
73   - else if (diff > 3599 && diff <= 86399) resStr = Math.ceil(diff / 3600) + '小时' + dirStr;
  76 + else if (diff >= 3600000 && diff < 86400000) resStr = Math.floor(diff / 3600000) + '小时' + dirStr;
74 77 // 多于23小时59分钟59秒,少于等于29天59分钟59秒
75   - else if (diff > 86399 && diff <= 2623859) resStr = Math.ceil(diff / 86400) + '天' + dirStr;
  78 + else if (diff >= 86400000 && diff < 2623860000) resStr = Math.floor(diff / 86400000) + '天' + dirStr;
76 79 // 多于29天59分钟59秒,少于364天23小时59分钟59秒,且传入的时间戳早于当前
77   - else if (diff > 2623859 && diff <= 31567859 && IS_EARLY) resStr = getDate(timeStamp);
  80 + else if (diff >= 2623860000 && diff <= 31567860000 && IS_EARLY) resStr = getDate(timeStamp);
78 81 else resStr = getDate(timeStamp, 'year');
79 82 return resStr;
80 83 };
... ...