Commit 37086acc1c32f856d2e4140eaf2d0200fe7e9eb0
1 parent
77669e5d
update Time
Showing
2 changed files
with
18 additions
and
17 deletions
Show diff stats
examples/routers/time.vue
1 | 1 | <template> |
2 | 2 | <div> |
3 | - <Time :time="1526608921" /> | |
4 | - <Time :time="1652839997" /> | |
5 | - <Time :time="ddd" :interval="1" /> | |
6 | - | |
7 | - <Time time="2018-06-20T13:09:14.000Z" /> | |
3 | + <Time :time="time1" /> | |
4 | + <br> | |
5 | + <Time :time="time2" /> | |
6 | + <br> | |
7 | + <Time :time="time3" :interval="60" /> | |
8 | 8 | </div> |
9 | 9 | </template> |
10 | 10 | <script> |
11 | 11 | export default { |
12 | 12 | data () { |
13 | 13 | return { |
14 | - ddd: new Date() | |
15 | - }; | |
16 | - }, | |
17 | - computed: {}, | |
18 | - methods: {} | |
19 | - }; | |
20 | -</script> | |
21 | 14 | \ No newline at end of file |
15 | + time1: (new Date()).getTime() - 60 * 1 * 1000, | |
16 | + time2: (new Date()).getTime() - 86400 * 3 * 1000, | |
17 | + time3: new Date() | |
18 | + } | |
19 | + } | |
20 | + } | |
21 | +</script> | ... | ... |
src/components/time/time.js
... | ... | @@ -13,7 +13,7 @@ |
13 | 13 | * @returns {Boolean} 传入的时间戳是否早于当前时间戳 |
14 | 14 | */ |
15 | 15 | const isEarly = (timeStamp, currentTime) => { |
16 | - return timeStamp < currentTime; | |
16 | + return timeStamp <= currentTime; | |
17 | 17 | }; |
18 | 18 | |
19 | 19 | /** |
... | ... | @@ -65,13 +65,14 @@ export const getRelativeTime = timeStamp => { |
65 | 65 | let resStr = ''; |
66 | 66 | const dirStr = IS_EARLY ? '前' : '后'; |
67 | 67 | // 少于等于59秒 |
68 | - if (diff <= 59) resStr = parseInt(diff) + '秒' + dirStr; | |
68 | + if (diff < 1) resStr = '刚刚'; | |
69 | + else if (diff <= 59) resStr = parseInt(diff) + '秒' + dirStr; | |
69 | 70 | // 多于59秒,少于等于59分钟59秒 |
70 | - else if (diff > 59 && diff <= 3599) resStr = Math.floor(diff / 60) + '分钟' + dirStr; | |
71 | + else if (diff > 59 && diff <= 3599) resStr = Math.ceil(diff / 60) + '分钟' + dirStr; | |
71 | 72 | // 多于59分钟59秒,少于等于23小时59分钟59秒 |
72 | - else if (diff > 3599 && diff <= 86399) resStr = Math.floor(diff / 3600) + '小时' + dirStr; | |
73 | + else if (diff > 3599 && diff <= 86399) resStr = Math.ceil(diff / 3600) + '小时' + dirStr; | |
73 | 74 | // 多于23小时59分钟59秒,少于等于29天59分钟59秒 |
74 | - else if (diff > 86399 && diff <= 2623859) resStr = Math.floor(diff / 86400) + '天' + dirStr; | |
75 | + else if (diff > 86399 && diff <= 2623859) resStr = Math.ceil(diff / 86400) + '天' + dirStr; | |
75 | 76 | // 多于29天59分钟59秒,少于364天23小时59分钟59秒,且传入的时间戳早于当前 |
76 | 77 | else if (diff > 2623859 && diff <= 31567859 && IS_EARLY) resStr = getDate(timeStamp); |
77 | 78 | else resStr = getDate(timeStamp, 'year'); | ... | ... |