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
How to use function from another file golang?
How to check pointer or interface is nil?
How to append text to a file in Golang?
Get current date and time in various format in golang
Golang import package inside package
Example of Pointers with Struct
Data encryption with AES-GCM
Regular expression to extract text between square brackets
How to update content of a text file?
How to trim leading and trailing white spaces of a string in Golang?