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
+10.7k Golang : Pagination with go-paginator configuration example
+14.2k Golang : Delete certain files in a directory
+4.4k Golang : Return multiple values from function
+7.5k Golang : How to find out similarity between two strings with Jaro-Winkler Distance?
+53.2k Golang : Unmarshal JSON from http response
+7.8k Golang : Heap sort example
+7.4k Golang : Generate Datamatrix barcode
+32.2k Golang : Create x509 certificate, private and public keys
+4.8k Linux : Disable and enable IPv4 forwarding
+16.1k Golang : Multi threading or run two processes or more example
+17.8k Golang : Get RGBA values of each image pixel
+8.3k Golang : Changing a RGBA image number of channels with OpenCV