extend RelativeTime function

This commit is contained in:
perennial
2024-12-05 07:07:43 +11:00
parent 81dd0b4e27
commit cdf121aaa0
+29 -2
View File
@@ -224,8 +224,35 @@ func RelativeTime(date time.Time) string {
return fmt.Sprintf("%d days ago", days)
}
// Older than a week
return date.Format("2 January 2006 3:04 PM")
// Within last month
if duration < 31*24*time.Hour {
weeks := int(duration.Hours() / (24 * 7))
if weeks == 1 {
return "1 week ago"
}
return fmt.Sprintf("%d weeks ago", weeks)
}
// Check months difference
months := int(now.Month() - date.Month())
yearDiff := now.Year() - date.Year()
if yearDiff > 0 {
months += yearDiff * 12
}
if months < 12 {
if months == 1 {
return "1 month ago"
}
return fmt.Sprintf("%d months ago", months)
}
// Years
years := yearDiff
if years == 1 {
return "1 year ago"
}
return fmt.Sprintf("%d years ago", years)
}
// CreatePaginator generates pagination data based on the current page and maximum number of pages.