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
+11.8k How to tell if a binary(executable) file or web application is built with Golang?
+6.4k WARNING: UNPROTECTED PRIVATE KEY FILE! error message
+9.2k Golang : Gonum standard normal random numbers example
+11.1k Golang : Web routing/multiplex example
+9.2k Golang : Intercept and compare HTTP response code example
+8.4k Useful methods to access blocked websites
+9.9k Golang : Format strings to SEO friendly URL example
+22.3k Golang : Print leading(padding) zero or spaces in fmt.Printf?
+30.6k Get client IP Address in Go
+5.3k Python : Create Whois client or function example
+14.9k Golang : Find commonalities in two slices or arrays example
+7k Golang : Normalize email to prevent multiple signups example