Go - Current Time Parts - year, month, day, hour, minute, second, and nanosecond



In the example, we print the year, month, day, hour, minute, second, and nanosecond of current time in Golang.

Go - Current Time Parts

In the example, we print the year, month, day, hour, minute, second, and nanosecond with the corresponding functions separately.

Let's create a file named "go_example.go" and add the following source code to it:


package main

import (
    "fmt"
    "time"
)

func main() {

    now := time.Now()

    fmt.Println("Year:", now.Year())
    fmt.Println("Month:", now.Month())
    fmt.Println("Day:", now.Day())
    fmt.Println("Hour:", now.Hour())
    fmt.Println("Minute:", now.Minute())
    fmt.Println("Second:", now.Second())
    fmt.Println("Nanosecond:", now.Nanosecond())
}

Run the following command to execute the go_example.go file:

G:\GoLang\examples>go run go_example.go
Year: 2021
Month: June
Day: 21
Hour: 17
Minute: 12
Second: 3
Nanosecond: 132324900

Comments