Welcome To Golang By Example

Menu
  • Home
  • Blog
Menu

Represent Date of Birth in Go (Golang)

Posted on January 22, 2023January 22, 2023 by admin

time.Date function of time package can be used to create a particular date that can represent a DOB.

See below example

  • getDOB is the function that takes in a year, month and day and returns a date.
package main
import (
"fmt"
"time"
)
const (
//TimeFormat1 to format date into
TimeFormat1 = "2006-01-02"
//TimeFormat2 Other format to format date time
TimeFormat2 = "January 02, 2006"
)
func main() {
dob := getDOB(2011, 4, 2)
fmt.Println(dob.Format(TimeFormat1))
fmt.Println(dob.Format(TimeFormat2))
}
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:

2011-04-02
April 02, 2011
  • date
  • dob
  • package
  • Popular Articles

    Golang Comprehensive Tutorial Series

    All Design Patterns in Go (Golang)

    Slice in golang

    Variables in Go (Golang) – Complete Guide

    OOP: Inheritance in GOLANG complete guide

    Using Context Package in GO (Golang) – Complete Guide

    All data types in Golang with examples

    Understanding time and date in Go (Golang) – Complete Guide

    ©2023 Welcome To Golang By Example | Design: Web XP