How to know the age from birth date

1,740 views
Skip to first unread message

Emilio García-Pumarino Álvarez

unread,
Jul 15, 2014, 3:27:06 PM7/15/14
to golan...@googlegroups.com
Hi!
I need know the age from birth date. Anyone know any library for do it? If not there are library for do it, how I can calculate it?

Thanks!

Emilio

egon

unread,
Jul 15, 2014, 4:31:26 PM7/15/14
to golan...@googlegroups.com
On Tuesday, 15 July 2014 22:27:06 UTC+3, Emilio García-Pumarino Álvarez wrote:
Hi!
I need know the age from birth date. Anyone know any library for do it? If not there are library for do it, how I can calculate it?

func age(birthday time.Time) int {
now := time.Now()
years := now.Year() - birthday.Year()
if now.YearDay() < birthday.YearDay(){
years--
}
return years
}


Thanks!

Emilio

Carlos Castillo

unread,
Jul 15, 2014, 4:36:16 PM7/15/14
to golan...@googlegroups.com
Go has a time package: http://golang.org/pkg/time/

Here's a most likely un-useful example: http://play.golang.org/p/tMvS17UqVg

How you want to determine "age" will guide how you use the time package to compute the answer. Egon's code is what people generally think of as "age" w.r.t. birthdays.

Also, this sounds like a homework question.

Emilio García-Pumarino Álvarez

unread,
Jul 15, 2014, 6:59:13 PM7/15/14
to golan...@googlegroups.com
Thanks all!! 
Finally, I get the function of Egon.

Carlos, yes, i'm practice with golang. I need study more it. Thanks!

Best,
Emilio

artem...@gmail.com

unread,
Dec 13, 2017, 8:47:05 AM12/13/17
to golang-nuts
Egon function will not handle Leap year properly comparing YearDay()
You must firstly compare month then day of month
Reply all
Reply to author
Forward
0 new messages