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 Unmarshal nested JSON structure?
Regular expression to extract text between square brackets
How to write backslash in Golang string?
Example: ReadAll, ReadDir, and ReadFile from IO Package
Find length of Channel, Pointer, Slice, String and Map
Subtract N number of Year, Month, Day, Hour, Minute, Second, Millisecond, Microsecond and Nanosecond to current date-time.
User Defined Function Types in Golang
How to use a mutex to define critical sections of code and fix race conditions?
How to convert Boolean Type to String in Go?
How can I convert a string variable into Boolean, Integer or Float type in Golang?