// 时间格式化函数
private formatTime(time: Date): string {
  const now = new Date();
  const diffInMs = now.getTime() - time.getTime();
  const diffInMinutes = Math.floor(diffInMs / (1000 * 60));
  const diffInHours = Math.floor(diffInMs / (1000 * 60 * 60));
  const diffInDays = Math.floor(diffInMs / (1000 * 60 * 60 * 24));

  if (diffInMinutes < 1) {
    return "刚刚";
  } else if (diffInMinutes < 30) {
    return `${diffInMinutes}分钟前`;
  } else if (diffInMinutes < 60) {
    return "半小时前";
  } else if (diffInHours < 24) {
    if (diffInHours === 1) {
      return "1小时前";
    } else {
      return `${diffInHours}小时前`;
    }
  } else if (diffInDays === 1) {
    return "昨天";
  } else if (diffInDays < 7) {
    return `${diffInDays}天前`;
  } else {
    // 超过一周显示具体日期
    return `${time.getFullYear()}-${(time.getMonth() + 1).toString().padStart(2, '0')}-${time.getDate()
      .toString()
      .padStart(2, '0')}`;
  }
}
Logo

开源鸿蒙跨平台开发社区汇聚开发者与厂商,共建“一次开发,多端部署”的开源生态,致力于降低跨端开发门槛,推动万物智联创新。

更多推荐