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
+6.8k Golang : How to determine if request or crawl is from Google robots
+16.5k Golang : convert string or integer to big.Int type
+23.8k Golang : Read a file into an array or slice example
+24.2k Golang : Fix type interface{} has no field or no methods and type assertions example
+13.6k Golang : Get constant name from value
+10.7k Generate Random number with math/rand in Go
+8.8k Golang : Another camera capture GUI application with GTK and OpenCV
+12.3k Golang : Pagination with go-paginator configuration example
+16.9k Golang : Generate QR codes for Google Authenticator App and fix "Cannot interpret QR code" error
+9.3k Golang : io.Reader causing panic: runtime error: invalid memory address or nil pointer dereference
+23.6k Golang : Check if element exist in map