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.6k Golang : Drop cookie to visitor's browser and http.SetCookie() example
+13.9k Golang : How to determine if a year is leap year?
+5.9k Facebook : How to force facebook to scrape latest URL link data?
+4.9k Javascript : How to get width and height of a div?
+13.7k Golang : Image to ASCII art example
+18.2k Golang : Put UTF8 text on OpenCV video capture image frame
+43.5k Golang : Get hardware information such as disk, memory and CPU usage
+20.2k Golang : Convert seconds to human readable time format example
+19.6k Golang : Close channel after ticker stopped example
+15.7k Golang : Get checkbox or extract multipart form data value example
+16.9k Golang : Set up source IP address before making HTTP request
+29.3k Golang : Save map/struct to JSON or XML file