时间戳转换为日期格式(天,小时,分,秒)

相关Function

 computeTime(timestamp: number) {
    
    
    const dateTypeArr = ['days ago', 'hours ago', 'minutes ago'];
    const now = Date.now();
    const duration = (now - timestamp) / 1000;  // 计算当前时间戳与传入时间戳的差值
    const days = Math.floor(duration / 60 / 60 / 24);
    const hours = Math.floor((duration / 60 / 60) % 24);
    const mins = Math.floor((duration / 60) % 60);
    const seconds = Math.floor(duration % 60);
   
    const timeArr = [days, hours, mins, seconds];
    const index = timeArr.findIndex((item) => item > 0);
    if (index === timeArr.length - 1 || index === -1)
      return 'In few seconds ago'; 
    return `${
      
      timeArr[index]} ${
      
      dateTypeArr[index]}`;
  }
// 計算時間并返回boolean
  computeTime(timestamp: number) {
    
    
    const now = Date.now();
    const duration = (now - timestamp) / 1000;
    const oneHour = 60 * 60;  //计算时间差是否在一小时之内
    if (duration >= oneHour) {
    
    
      return false;
    }
    return true;
  }

猜你喜欢

转载自blog.csdn.net/weixin_45680024/article/details/126348167