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.4k Golang : Create zip/ePub file without compression(use Store algorithm)
+9.3k Generate Random number with math/rand in Go
+6.7k Gogland : Where to put source code files in package directory for rookie
+10.4k Get form post value in Go
+10.7k Golang : Clean formatting/indenting or pretty print JSON result
+9.3k Golang : Meaning of omitempty in struct's field tag
+26.6k Get file path of temporary file in Go
+21.7k Golang : Randomly pick an item from a slice/array example
+4.7k Linux/Unix/PHP : Restart PHP-FPM
+4.6k Golang : ROT32768 (rotate by 0x80) UTF-8 strings example
+9k Android Studio : Simple input textbox and intercept key example