Ad space (banner)
๐ŸนGo Lessons
Lesson 36 / 68

Building and Displaying Dates (Basic Year/Month/Day Operations)

In this lesson you will learn the basics of working with dates in Go, so you can build a date and find its day of the week. This is for anyone searching for "Go time.Date how to use".

time.Date() lets you assemble and work with a specific date. Reading the day of the week and changing the display format are both straightforward.

The sample code creates a date with time.Date(2026, time.August, 9, 0, 0, 0, 0, time.UTC) and reads the parts with d.Year(), d.Month(), and d.Day(). Go's time package lets you name months as constants such as time.August, which reads better than a bare number.

A common stumbling block for beginners is time zones. Without stating one explicitly, as with time.UTC, you can end up displaying an unintended time. For zone-aware date handling you combine this with time.LoadLocation.

In real-world development date handling is needed widely, from displaying posting timestamps to calculating deadlines - for example using the Sub() method to find the gap between two dates.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Go directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)