×

go · level 2 · unit 5 · lesson 4

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

The time toolbox builds dates, formats them and does sums.

time.Date(2026, time.March, 14, 18, 30, 0, 0, time.UTC) builds a : year, month, day, hour, minute, second, a tiny fraction of a second, then the time zone. A fixed date like this gives the same answer on every run.

To show a date, hand Format a written with Go's reference date, Monday 2 January 2006 at 15:04. party.Format("Mon 2 Jan 2006") gives Sat 14 Mar 2026. Move the pieces around for any style you like.

party.AddDate(0, 0, 7) adds years, months and days. later.Sub(party) gives a , and Hours() turns it into a number of hours. Before and After compare two moments, and Weekday names the day.

package main
import "fmt"
import "time"
func main() {
	party := time.Date(2026, time.March, 14, 18, 30, 0, 0, time.UTC)
	fmt.Println(party.Format("Mon 2 Jan 2006 at 15:04"))
	nextWeek := party.AddDate(0, 0, 7)
	fmt.Println(nextWeek.Format("2 January"), nextWeek.Weekday())
	gap := nextWeek.Sub(party)
	fmt.Println(gap, gap.Hours()/24, "days")
	fmt.Println(party.Before(nextWeek))
}

Every layout uses those exact reference numbers, so 01 means the month and 02 means the day.