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
+16.6k Golang : Send email and SMTP configuration example
+21.6k Curl usage examples with Golang
+9.2k Golang : How to use Gorilla webtoolkit context package properly
+11.7k Use systeminfo to find out installed Windows Hotfix(s) or updates
+6.4k PHP : How to handle URI or URL with non-ASCII characters such as Chinese/Japanese/Korean(CJK) ?
+5.1k Google : Block or disable caching of your website content
+28.3k Golang : Connect to database (MySQL/MariaDB) server
+31.9k Golang : How to convert(cast) string to IP address?
+8.5k Your page has meta tags in the body instead of the head
+7.6k Golang : How to detect if a sentence ends with a punctuation?
+6.9k Golang : Calculate pivot points for a cross
+21.9k Golang : How to reverse slice or array elements order