8

Get year, month, day from time

 3 years ago
source link: https://yourbasic.org/golang/day-month-year-from-time/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Get year, month, day from time

yourbasic.org/golang
mark-date-calendar.jpg

The Date function returns the year, month and day of a time.Time.

func (t Time) Date() (year int, month Month, day int)

In use:

year, month, day := time.Now().Date()
fmt.Println(year, month, day)      // For example 2009 November 10
fmt.Println(year, int(month), day) // For example 2009 11 10

You can also extract the information with seperate calls:

t := time.Now()
year := t.Year()   // type int
month := t.Month() // type time.Month
day := t.Day()     // type int

The time.Month type specifies a month of the year (January = 1, …).

type Month int

const (
	January Month = 1 + iota
	February
	March
	April
	May
	June
	July
	August
	September
	October
	November
	December
)

Share:             


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK