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
+6.2k Golang : Grab news article text and use NLP to get each paragraph's sentences
+7.2k Restart Apache or Nginx web server without password prompt
+15.4k Golang : How to get Unix file descriptor for console and file
+16.6k Golang : Execute terminal command to remote machine example
+8.9k Golang : On lambda, anonymous, inline functions and function literals
+7.7k Golang : get the current working directory of a running program
+5.6k Clean up Visual Studio For Mac installation failed disk full problem
+22.8k Golang : Round float to precision example
+8.7k Python : Fix SyntaxError: Non-ASCII character in file, but no encoding declared
+5.4k Golang : Return multiple values from function
+15.3k Golang : Save(pipe) HTTP response into a file
+18.8k Golang : Iterating Elements Over A List