Get Year, Month, Day, Hour, Min and Second from current date and time.
Below program use Parse function to get Year, Month, Day, Hour, Min and Second from current date and time. The Clock function used to returns the hour, minute, and second within the day specified by time T.
Example
package main
import (
"fmt"
"time"
)
func main() {
currentTime := time.Now()
fmt.Println("\n######################################\n")
fmt.Println(currentTime.Format("2006-01-02 15:04:05"))
fmt.Println("\n######################################\n")
timeStampString := currentTime.Format("2006-01-02 15:04:05")
layOut := "2006-01-02 15:04:05"
timeStamp, err := time.Parse(layOut, timeStampString)
if err != nil {
fmt.Println(err)
}
hr, min, sec := timeStamp.Clock()
fmt.Println("Year :", currentTime.Year())
fmt.Println("Month :", currentTime.Month())
fmt.Println("Day :", currentTime.Day())
fmt.Println("Hour :", hr)
fmt.Println("Min :", min)
fmt.Println("Sec :", sec)
fmt.Println("\n######################################\n")
year, month, day := time.Now().Date()
fmt.Println("Year :", year)
fmt.Println("Month :", month)
fmt.Println("Day :", day)
fmt.Println("\n######################################\n")
t := time.Now()
y := t.Year()
mon := t.Month()
d := t.Day()
h := t.Hour()
m := t.Minute()
s := t.Second()
n := t.Nanosecond()
fmt.Println("Year :",y)
fmt.Println("Month :",mon)
fmt.Println("Day :",d)
fmt.Println("Hour :",h)
fmt.Println("Minute :",m)
fmt.Println("Second :",s)
fmt.Println("Nanosec:",n)
}
Output
######################################
2017-08-27 18:11:54
######################################
Year : 2017
Month : August
Day : 27
Hour : 18
Min : 11
Sec : 54
######################################
Year : 2017
Month : August
Day : 27
######################################
Year : 2017
Month : August
Day : 27
Hour : 18
Minute : 11
Second : 54
Nanosec: 319513100
Most Helpful This Week
Example: Arrays of Arrays, Arrays of Slices, Slices of Arrays and Slices of Slices
Regular expression to extract DNS host-name or IP Address from string
How to compare equality of struct, slice and map?
Simple function with return value in Golang
How to print string with double quote in Go?
How to read names of all files and folders in current directory?
Dereferencing a pointer from another package
Convert specific UTC date time to PST, HST, MST and SGT
Normal function parameter with variadic function parameter
Golang Functions Returning Multiple Values