Golang : How to get year, month and day?
Problem :
You want to extract the year, month and day from today's date. How to do that?
Solution :
Use the time.Now().Date()
function to get year, month and day from today's date.
For example :
package main
import (
"fmt"
"time"
)
func main() {
year, month, day := time.Now().Date()
fmt.Println("Year : ", year)
fmt.Println("Month : ", month)
fmt.Println("Day : ", day)
if month == time.November && day == 10 {
fmt.Println("Happy Go day!")
}
}
Sample output :
Year : 2015
Month : August
Day : 3
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
+18.9k Golang : Calculate entire request body length during run time
+16.9k Golang : How to tell if a file is compressed either gzip or zip ?
+11.1k Golang : Byte format example
+7.9k Golang : Append and add item in slice
+11k Golang : How to use if, eq and print properly in html template
+17.3k Golang : [json: cannot unmarshal object into Go value of type]
+20.4k PHP : Convert(cast) int to double/float
+13.8k Golang : Fix image: unknown format error
+9.2k Golang : Convert(cast) string to int64
+22.6k Golang : Calculate time different
+9.6k Golang : Sort and reverse sort a slice of integers
+12.9k Golang : Convert(cast) int to int64