age Archives - Welcome To Golang By Example https://vikasboss.github.io/tag/age/ Fri, 24 Jan 2020 03:06:46 +0000 en-US hourly 1 https://wordpress.org/?v=6.8.1 https://i0.wp.com/golangbyexamples.com/wp-content/uploads/2021/05/cropped-go_border-1.png?fit=32%2C32&ssl=1 age Archives - Welcome To Golang By Example https://vikasboss.github.io/tag/age/ 32 32 159787465 Get age given a DOB in Go (Golang) https://vikasboss.github.io/get-age-given-dob-go/ https://vikasboss.github.io/get-age-given-dob-go/#respond Thu, 23 Jan 2020 16:36:42 +0000 https://vikasboss.github.io/?p=1209 This tutorial will talk about how given a date of birth we can compute the age of a person. go-age comes to our rescue for doing that. It also takes into account...

The post Get age given a DOB in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
This tutorial will talk about how given a date of birth we can compute the age of a person. go-age comes to our rescue for doing that. It also takes into account the complexities of leap year while calculating age.

See a below working example:

package main

import (
    "fmt"
    "time"
    age "github.com/bearbin/go-age"
)

func main() {
    dob := getDOB(2011, 4, 2)
    fmt.Printf("Age is %d\n", age.Age(dob))
}

func getDOB(year, month, day int) time.Time {
    dob := time.Date(year, time.Month(month), day, 0, 0, 0, 0, time.UTC)
    return dob
}

Output:

Age is 8

The post Get age given a DOB in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
https://vikasboss.github.io/get-age-given-dob-go/feed/ 0 1209