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
+18.6k Golang : Populate dropdown with html/template example
+16.6k Golang : Get number of CPU cores
+11.8k Golang : convert(cast) string to integer value
+4.4k Unix/Linux : How to pipe/save output of a command to file?
+30.8k Golang : How to convert(cast) string to IP address?
+14.6k JavaScript/JQuery : Detect or intercept enter key pressed example
+15.8k Golang : Find out mime type from bytes in buffer
+5.4k Golang : Struct field tags and what is their purpose?
+7.3k Gogland : Where to put source code files in package directory for rookie
+5.5k Golang : List all packages and search for certain package
+5.2k PHP : Fix Call to undefined function curl_init() error
+40.6k Golang : How do I convert int to uint8?