Time and Duration
Represent moments and intervals.
Time and Duration is a free Go Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
The time Package
Go's time package represents moments in time and spans between them.
time.Timeis a specific instant.time.Durationis a length of time.
package main
import (
"fmt"
"time"
)
func main() {
var d time.Duration = 2 * time.Second
fmt.Println(d)
}Getting the Current Time
time.Now() returns the current instant as a time.Time. (Its exact value depends on when the program runs.)
package main
import (
"fmt"
"time"
)
func main() {
now := time.Now()
fmt.Println(now.Year())
}Building a Specific Time
time.Date constructs an exact moment from its parts: year, month, day, hour, minute, second, nanosecond, and location.
package main
import (
"fmt"
"time"
)
func main() {
t := time.Date(2026, time.May, 30, 9, 0, 0, 0, time.UTC)
fmt.Println(t)
}Reading Time Components
A time.Time exposes methods like Year, Month, Day, Hour, and Weekday.
package main
import (
"fmt"
"time"
)
func main() {
t := time.Date(2026, time.May, 30, 0, 0, 0, 0, time.UTC)
fmt.Println(t.Month(), t.Day(), t.Weekday())
}Duration Constants
Build durations by multiplying the unit constants: time.Hour, time.Minute, time.Second, time.Millisecond.
package main
import (
"fmt"
"time"
)
func main() {
d := 1*time.Hour + 30*time.Minute
fmt.Println(d)
}Converting Durations
Methods like Seconds(), Minutes(), and Hours() return the duration as a float64 in that unit.
package main
import (
"fmt"
"time"
)
func main() {
d := 90 * time.Minute
fmt.Println(d.Hours())
fmt.Println(d.Seconds())
}Adding Time
t.Add(d) returns a new time shifted by a duration. The original value is unchanged because time.Time is a value type.
package main
import (
"fmt"
"time"
)
func main() {
t := time.Date(2026, time.May, 30, 9, 0, 0, 0, time.UTC)
later := t.Add(2 * time.Hour)
fmt.Println(later.Hour())
}Adding Calendar Units
t.AddDate(years, months, days) shifts by calendar amounts, correctly handling month lengths and leap years.
package main
import (
"fmt"
"time"
)
func main() {
t := time.Date(2026, time.January, 31, 0, 0, 0, 0, time.UTC)
next := t.AddDate(0, 1, 0)
fmt.Println(next.Month(), next.Day())
}Difference Between Times
t2.Sub(t1) returns the Duration between two times. A negative result means t2 is earlier.
package main
import (
"fmt"
"time"
)
func main() {
a := time.Date(2026, time.May, 30, 9, 0, 0, 0, time.UTC)
b := time.Date(2026, time.May, 30, 12, 0, 0, 0, time.UTC)
fmt.Println(b.Sub(a))
}Comparing Times
Use Before, After, and Equal to compare instants. Avoid == because it also compares the internal location.
package main
import (
"fmt"
"time"
)
func main() {
a := time.Date(2026, 5, 30, 9, 0, 0, 0, time.UTC)
b := time.Date(2026, 5, 30, 10, 0, 0, 0, time.UTC)
fmt.Println(a.Before(b))
}Unix Timestamps
t.Unix() gives seconds since January 1, 1970. time.Unix(sec, 0) converts back to a time.Time.
package main
import (
"fmt"
"time"
)
func main() {
t := time.Date(2026, 5, 30, 0, 0, 0, 0, time.UTC)
secs := t.Unix()
fmt.Println(time.Unix(secs, 0).UTC().Year())
}Quick Check
What does t2.Sub(t1) return?
Recap: Time and Duration
You learned the core time model:
time.Timeis an instant;time.Durationis a span.- Create with
NowandDate; shift withAddandAddDate. - Measure with
Suband compare withBefore/After.
package main
import (
"fmt"
"time"
)
func main() {
start := time.Date(2026, 5, 30, 8, 0, 0, 0, time.UTC)
end := start.Add(45 * time.Minute)
fmt.Println(end.Sub(start).Minutes(), "minutes")
}Frequently asked questions
Is the “Time and Duration” lesson free?
Yes — the full text of “Time and Duration” is free to read here on the web, and the Go Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Go Academy course, upgrade to CoddyKit PRO.
What will I learn in “Time and Duration”?
Represent moments and intervals. You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Go Academy?
No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Time and Duration” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Go Academy lesson?
Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Time and Duration
- Formatting and Parsing
- Timers and Tickers
- Time Zones