Time
// Importing necessary packages.
import (
"fmt"
"time"
)
// The main function, where the execution of the program begins.
func main() {
// Creating an alias for fmt.Println for brevity.
p := fmt.Println
// Getting the current time.
now := time.Now()
p(now)
// Creating a specific date and time.
then := time.Date(
2009, 11, 17, 20, 34, 58, 651387237, time.UTC)
p(then)
// Extracting various components of the time.
p(then.Year())
p(then.Month())
p(then.Day())
p(then.Hour())
p(then.Minute())
p(then.Second())
p(then.Nanosecond())
p(then.Location())
// Determining the weekday.
p(then.Weekday())
// Comparing two times.
p(then.Before(now))
p(then.After(now))
p(then.Equal(now))
// Calculating the time difference.
diff := now.Sub(then)
p(diff)
// Extracting components of the time difference.
p(diff.Hours())
p(diff.Minutes())
p(diff.Seconds())
p(diff.Nanoseconds())
// Adding and subtracting time durations.
p(then.Add(diff))
p(then.Add(-diff))
}Output
Last updated