Blame view

src/components/time/time.js 3.58 KB
de30f1b3   梁灏   update Time func
1
2
3
4
5
  /**
   * @param {Number} timeStamp 判断时间戳格式是否是毫秒
   * @returns {Boolean}
   */
  // const isMillisecond = timeStamp => {
28295360   梁灏   update Time logic
6
7
8
  //     const timeStr = String(timeStamp)
  //     return timeStr.length > 10
  // }
eb37e68c   梁灏   update Time
9
  
de30f1b3   梁灏   update Time func
10
11
12
13
14
15
  /**
   * @param {Number} timeStamp 传入的时间戳
   * @param {Number} currentTime 当前时间时间戳
   * @returns {Boolean} 传入的时间戳是否早于当前时间戳
   */
  const isEarly = (timeStamp, currentTime) => {
37086acc   梁灏   update Time
16
      return timeStamp <= currentTime;
de30f1b3   梁灏   update Time func
17
  };
eb37e68c   梁灏   update Time
18
  
de30f1b3   梁灏   update Time func
19
20
21
22
23
24
25
26
  /**
   * @param {Number} num 数值
   * @returns {String} 处理后的字符串
   * @description 如果传入的数值小于10,即位数只有1位,则在前面补充0
   */
  const getHandledValue = num => {
      return num < 10 ? '0' + num : num;
  };
eb37e68c   梁灏   update Time
27
  
de30f1b3   梁灏   update Time func
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
  /**
   * @param {Number} timeStamp 传入的时间戳
   * @param {Number} startType 要返回的时间字符串的格式类型,传入'year'则返回年开头的完整时间
   */
  const getDate = (timeStamp, startType) => {
      const d = new Date(timeStamp * 1000);
      const year = d.getFullYear();
      const month = getHandledValue(d.getMonth() + 1);
      const date = getHandledValue(d.getDate());
      const hours = getHandledValue(d.getHours());
      const minutes = getHandledValue(d.getMinutes());
      const second = getHandledValue(d.getSeconds());
      let resStr = '';
      if (startType === 'year') resStr = year + '-' + month + '-' + date + ' ' + hours + ':' + minutes + ':' + second;
      else resStr = month + '-' + date + ' ' + hours + ':' + minutes;
      return resStr;
  };
eb37e68c   梁灏   update Time
45
  
de30f1b3   梁灏   update Time func
46
47
48
49
  /**
   * @param {String|Number} timeStamp 时间戳
   * @returns {String} 相对时间字符串
   */
8cebd97f   kazuki watanabe   make Time Compone...
50
  export const getRelativeTime = (timeStamp, locale) => {
de30f1b3   梁灏   update Time func
51
      // 判断当前传入的时间戳是秒格式还是毫秒
be772972   梁灏   update Time
52
      // const IS_MILLISECOND = true;
de30f1b3   梁灏   update Time func
53
      // 如果是毫秒格式则转为秒格式
d2041471   梁灏   update Time
54
      // if (IS_MILLISECOND) Math.floor(timeStamp /= 1000);
de30f1b3   梁灏   update Time func
55
      // 传入的时间戳可以是数值或字符串类型,这里统一转为数值类型
d2041471   梁灏   update Time
56
      // timeStamp = Number(timeStamp);
de30f1b3   梁灏   update Time func
57
      // 获取当前时间时间戳
d2041471   梁灏   update Time
58
59
60
      // const currentTime = Math.floor(Date.parse(new Date()) / 1000);
      const currentTime = (new Date()).getTime();
  
de30f1b3   梁灏   update Time func
61
62
63
64
65
66
67
      // 判断传入时间戳是否早于当前时间戳
      const IS_EARLY = isEarly(timeStamp, currentTime);
      // 获取两个时间戳差值
      let diff = currentTime - timeStamp;
      // 如果IS_EARLY为false则差值取反
      if (!IS_EARLY) diff = -diff;
      let resStr = '';
8cebd97f   kazuki watanabe   make Time Compone...
68
      let dirStr = IS_EARLY ? (locale('i.time.before') || '前') : (locale('i.time.after') || '后');
d2041471   梁灏   update Time
69
  
8cebd97f   kazuki watanabe   make Time Compone...
70
      if (diff < 1000) resStr = locale('i.time.just') || '刚刚';
de30f1b3   梁灏   update Time func
71
      // 少于等于59秒
8cebd97f   kazuki watanabe   make Time Compone...
72
      else if (diff < 60000) resStr = parseInt(diff / 1000) + (locale('i.time.seconds') || '秒') + dirStr;
de30f1b3   梁灏   update Time func
73
      // 多于59秒,少于等于59分钟59秒
8cebd97f   kazuki watanabe   make Time Compone...
74
      else if (diff >= 60000 && diff < 3600000) resStr = Math.floor(diff / 60000) + (locale('i.time.minutes') || '分钟') + dirStr;
de30f1b3   梁灏   update Time func
75
      // 多于59分钟59秒,少于等于23小时59分钟59秒
8cebd97f   kazuki watanabe   make Time Compone...
76
      else if (diff >= 3600000 && diff < 86400000) resStr = Math.floor(diff / 3600000) + (locale('i.time.hours') || '小时') + dirStr;
de30f1b3   梁灏   update Time func
77
      // 多于23小时59分钟59秒,少于等于29天59分钟59秒
8cebd97f   kazuki watanabe   make Time Compone...
78
      else if (diff >= 86400000 && diff < 2623860000) resStr = Math.floor(diff / 86400000) + (locale('i.time.days') || '天') + dirStr;
28295360   梁灏   update Time logic
79
      // 多于29天59分钟59秒,少于364天23小时59分钟59秒,且传入的时间戳早于当前
d2041471   梁灏   update Time
80
      else if (diff >= 2623860000 && diff <= 31567860000 && IS_EARLY) resStr = getDate(timeStamp);
de30f1b3   梁灏   update Time func
81
82
      else resStr = getDate(timeStamp, 'year');
      return resStr;
eb37e68c   梁灏   update Time
83
84
  };
  
8cebd97f   kazuki watanabe   make Time Compone...
85
86
87
  export default function (timestamp, locale) {
      return getRelativeTime(timestamp, locale);
  }