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
+9.2k Golang : Convert(cast) string to int64
+86.3k Golang : How to convert character to ASCII and back
+22.8k Golang : Randomly pick an item from a slice/array example
+8.8k Golang : Get curl -I or head data from URL example
+14.3k Golang : Rename directory
+8.1k Golang : Number guessing game with user input verification example
+4.9k Golang : Customize scanner.Scanner to treat dash as part of identifier
+5.8k Golang : Grab news article text and use NLP to get each paragraph's sentences
+10.9k Golang : Proper way to test CIDR membership of an IP 4 or 6 address example
+18.3k Golang : Iterating Elements Over A List
+11.8k Golang : Pagination with go-paginator configuration example