How to get year, month, day, hour, minute and second from a date in Go

1 Answer

0 votes
package main
import (
         "fmt"
         "time"    
)
func main() {
    t := time.Date(2020, 8, 9, 12, 44, 31, 117, time.UTC) 
    fmt.Println(t)

    year := t.Year()
    month := t.Month()
    day := t.Day()
    hour := t.Hour()
    minute := t.Minute()
    second := t.Second()
    nanosecond := t.Nanosecond()
      
    fmt.Println("Year:", year)
    fmt.Println("Month:",month)
    fmt.Println("Day:", day)
    fmt.Println("Hour:", hour)
    fmt.Println("Minute:", minute)
    fmt.Println("Second:", second)
    fmt.Println("Nanosecond:", nanosecond)
}  


   
/*
run:
   
2020-08-09 12:44:31.000000117 +0000 UTC
Year: 2020
Month: August
Day: 9
Hour: 12
Minute: 44
Second: 31
Nanosecond: 117
 
*/

 



answered Aug 9, 2020 by avibootz
...