Golang : Find age or leap age from date of birth example
Alright, here is a simple tutorial on how to calculate a person's age by a given date of birth string. The code below will take a date of birth string input and spits out the equivalent age in years and if the date of birth is on 29th February, it will give the leap age as well.
Here you go!
package main
import (
"fmt"
"strconv"
"time"
)
func monthToInt(month string) int {
switch month {
case "January":
return 1
case "February":
return 2
case "March":
return 3
case "April":
return 4
case "May":
return 5
case "June":
return 6
case "July":
return 7
case "August":
return 8
case "September":
return 9
case "October":
return 10
case "November":
return 11
case "December":
return 12
default:
panic("Unrecognized month")
}
}
func ageFromDateOfBirth(dob string) (int, int) {
layOut := "02/01/2006" // dd/mm/yyyy
dobTime, err := time.Parse(layOut, dob)
if err != nil {
panic(err)
}
var ageYear, leapAge int
ageYear = time.Now().Year() - dobTime.Year()
// the trick here is to combine the day and month into an integer of string type
dobDayMonth, _ := strconv.Atoi(strconv.Itoa(dobTime.Day()) + strconv.Itoa(monthToInt(dobTime.Month().String())))
nowDayMonth, _ := strconv.Atoi(strconv.Itoa(time.Now().Day()) + strconv.Itoa(monthToInt(time.Now().Month().String())))
// if the DOB's day + month is larger than today's day + month
// then the age is still younger by 1 year
if dobDayMonth > nowDayMonth {
ageYear = ageYear - 1
}
if dobDayMonth == 292 { // dob on 29th Feb - leap year
leapAge = ageYear / 4
} else {
leapAge = 0
}
return ageYear, leapAge
}
func main() {
// dd/mm/yyyy format
dob := "28/10/1978"
age, _ := ageFromDateOfBirth(dob)
fmt.Println(dob, " age is : ", age)
dob1 := "01/10/1978"
age1, _ := ageFromDateOfBirth(dob1)
fmt.Println(dob1, " age is : ", age1)
dob2 := "02/05/1978"
age2, _ := ageFromDateOfBirth(dob2)
fmt.Println(dob2, " age is : ", age2)
dob3 := "03/05/1978"
age3, _ := ageFromDateOfBirth(dob3)
fmt.Println(dob3, " age is : ", age3)
// February 29, also known as leap day or leap year day,
// is a date added to most years that are divisible by 4,
// such as 2008, 2012, 2016, 2020, and 2024.
dob4 := "29/02/2008" // leap year age test
age4, leap := ageFromDateOfBirth(dob4)
fmt.Println(dob4, " age is : ", age4)
fmt.Println(dob4, " date of birth is 29th Feb and the leap age is : ", leap)
dob5 := "29/02/1936" // leap year age test
age5, leap1 := ageFromDateOfBirth(dob5)
fmt.Println(dob5, " age is : ", age5)
fmt.Println(dob5, " date of birth is 29th Feb and the leap age is : ", leap1)
}
Sample output:
$ date
Tue May 2 13:51:40 MYT 2017
$ ./agedob
28/10/1978 age is : 38
01/10/1978 age is : 38
02/05/1978 age is : 39
03/05/1978 age is : 38
29/02/2008 age is : 8
29/02/2008 date of birth is 29th Feb and the leap age is : 2
29/02/1936 age is : 80
29/02/1936 date of birth is 29th Feb and the leap age is : 20
Happy coding!
References:
See also : Golang : Format numbers to nearest thousands such as kilos millions billions and trillions
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+21.6k Golang : How to reverse slice or array elements order
+5k Golang : Issue HTTP commands to server and port example
+25.2k Golang : Daemonizing a simple web server process example
+7.3k Golang : Dealing with struct's private part
+13.1k Golang : Date and Time formatting
+9.2k Golang : Find the length of big.Int variable example
+19k Golang : Get host name or domain name from IP address
+24.3k Golang : How to print rune, unicode, utf-8 and non-ASCII CJK(Chinese/Japanese/Korean) characters?
+33.8k Golang : Create x509 certificate, private and public keys
+9.9k Golang : Edge detection with Sobel method
+8.6k Yum Error: no such table: packages
+14.7k Golang : Submit web forms without browser by http.PostForm example