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
+25.6k Golang : How to write CSV data to file
+31.3k Golang : bufio.NewReader.ReadLine to read file line by line
+12.6k Golang : Remove or trim extra comma from CSV
+6.7k Golang : Join lines with certain suffix symbol example
+9.4k Golang : Changing a RGBA image number of channels with OpenCV
+14.7k Golang : Find commonalities in two slices or arrays example
+25.3k Golang : Convert long hexadecimal with strconv.ParseUint example
+6.2k Apt-get to install and uninstall Golang
+15.8k Golang : Update database with GORM example
+7.6k Gogland : Where to put source code files in package directory for rookie
+11.2k Golang : How to pipe input data to executing child process?
+19.3k Golang : Fix cannot download, $GOPATH not set error