Skip to content

Commit

Permalink
Feat: 현재 시간을 기준으로 몇시간전인지 나타내는 Utility추가
Browse files Browse the repository at this point in the history
  • Loading branch information
GeonH0 committed Oct 8, 2024
1 parent 836a2fc commit d047ef6
Showing 1 changed file with 14 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,18 @@ extension DateFormatter {
formatter.locale = Locale(identifier: "en_US_POSIX")
return formatter
}()

static func timeAgoDisplay(from date: Date) -> String {
let calendar = Calendar.current
let now = Date()
let components = calendar.dateComponents([.hour, .day], from: date, to: now)

if let day = components.day, day >= 1 {
return "\(day)일 전"
} else if let hour = components.hour, hour >= 1 {
return "\(hour)시간 전"
} else {
return "방금 전"
}
}
}

0 comments on commit d047ef6

Please sign in to comment.