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
+8.5k Golang : Progress bar with ∎ character
+50.7k Golang : Disable security check for HTTPS(SSL) with bad or expired certificate
+15.4k Golang : How to convert(cast) IP address to string?
+8.7k Golang : Get final balance from bit coin address example
+13.8k Golang : Get current time
+7.4k Golang : Handling Yes No Quit query input
+12k Golang : md5 hash of a string
+10.4k Golang : How to unmarshal JSON inner/nested value and assign to specific struct?
+15.2k Golang : Find location by IP address and display with Google Map
+10.4k Generate Random number with math/rand in Go
+18.4k Golang : Send email with attachment
+5.7k Golang : Find change in a combination of coins example