Date and Time is one of the important topic in any programming language. In Go we can import the module named 'time' to work with Date and Time.
Let us start with the below example.
package main import "fmt" import "time" func main() { x := time.Now() fmt.Println(x) }
Now, if we dissect the output,
So, with the below statement,
We get the date and time in the variable, 'x' that has Year, Month, Day, Hour, Minute, Second and Microsecond.
So, you got the date in clubbed format. Now, what if you want everything separately.
Well! Go provides that way as well.
package main import "fmt" import "time" func main() { x := time.Now() year := x.Year() month := x.Month() day := x.Day() hour := x.Hour() minute := x.Minute() second := x.Second() nanoSecond := x.Nanosecond() fmt.Println("The full format is ", x) fmt.Println("The year is ",year) fmt.Println("The month is ",month) fmt.Println("The day is ",day) fmt.Println("The hour is ",hour) fmt.Println("The minute is ",minute) fmt.Println("The second is ",second) fmt.Println("The nanoSecond is ",nanoSecond) }
Now, if you look at the above output, we have separated the clubbed formatted date.
To display the year, you need to invoke 'x.Year()'.
Similar, logic applies for Month, Day, Hour, Minute, Second and Microsecond.