22 lines
649 B
Go
22 lines
649 B
Go
package utils
|
|
|
|
import "time"
|
|
|
|
// CalculateAge calculates the age based on a given birthdate and the current date in the format dd/mm/yy
|
|
// It adjusts for cases where the current date is before the birthday in the current year.
|
|
func CalculateAge(birthdate, today time.Time) int {
|
|
today = today.In(birthdate.Location())
|
|
ty, tm, td := today.Date()
|
|
today = time.Date(ty, tm, td, 0, 0, 0, 0, time.UTC)
|
|
by, bm, bd := birthdate.Date()
|
|
birthdate = time.Date(by, bm, bd, 0, 0, 0, 0, time.UTC)
|
|
if today.Before(birthdate) {
|
|
return 0
|
|
}
|
|
age := ty - by
|
|
anniversary := birthdate.AddDate(age, 0, 0)
|
|
if anniversary.After(today) {
|
|
age--
|
|
}
|
|
return age
|
|
} |