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
+8.6k Golang : How to protect your source code from client, hosting company or hacker?
+15.3k Golang :Trim white spaces from a string
+15.7k Golang : How to implement two-factor authentication?
+32.9k Golang : convert(cast) bytes to string
+5.8k Golang : How to setup a disk space used monitoring service with Telegram bot
+8.8k Golang : Populate slice with sequential integers example
+7k Golang : Scan files for certain pattern and rename part of the files
+26.1k Golang : Convert CSV data to JSON format and save to file
+4.3k Golang : Convert lines of string into list for delete and insert operation
+6.8k Golang : Shuffle strings array
+8.5k Golang : Detect Pascal, Kebab, Screaming Snake and Camel cases
+20.2k Golang : Get password from console input without echo or masked