date Archives - Welcome To Golang By Example https://vikasboss.github.io/tag/date/ Wed, 22 Jan 2020 03:12:30 +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 date Archives - Welcome To Golang By Example https://vikasboss.github.io/tag/date/ 32 32 159787465 Represent Date of Birth in Go (Golang) https://vikasboss.github.io/dob-golang/ https://vikasboss.github.io/dob-golang/#respond Wed, 22 Jan 2020 03:12:27 +0000 https://vikasboss.github.io/?p=1186 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...

The post Represent Date of Birth in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
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

The post Represent Date of Birth in Go (Golang) appeared first on Welcome To Golang By Example.

]]>
https://vikasboss.github.io/dob-golang/feed/ 0 1186