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
+12.5k Golang : Transform comma separated string to slice example
+17.3k Golang : Check if IP address is version 4 or 6
+14.3k Golang : Recombine chunked files example
+9k Golang : Handle sub domain with Gin
+5k Swift : Convert (cast) Float to Int or Int32 value
+12.6k Golang : Drop cookie to visitor's browser and http.SetCookie() example
+19.1k Mac OSX : Homebrew and Golang
+7k Golang : Validate credit card example
+5.6k Golang : Frobnicate or tweaking a string example
+8.8k Golang : GMail API create and send draft with simple upload attachment example
+15.2k Golang : Get query string value on a POST request
+12.2k Golang : Simple client-server HMAC authentication without SSL example