add function to calculate age
This commit is contained in:
parent
36d3f05a31
commit
90f6d26f6d
21
internal/utils/age.go
Normal file
21
internal/utils/age.go
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user