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
+44.6k Golang : Encode image to base64 example
+16k Golang : Get future or past hours, minutes or seconds
+4.6k Python : Print unicode escape characters and string
+10.8k Golang : Perform sanity checks on filename example
+4.3k JavaScript/JQuery : Redirect page examples
+5.7k Golang : Skip or discard items of non-interest when iterating example
+21.3k Golang : Set and Get HTTP request headers example
+25.2k Golang : Encrypt and decrypt data with AES crypto
+4.2k Golang : Display packages names during compilation
+4.3k Responsive Google Adsense
+34.3k Golang : Validate IP address
+16.7k Golang : Get all upper case or lower case characters from string example