Ad space (banner)
๐Ÿ”งC Lessons
Lesson 36 / 68

Building and Displaying Dates (struct tm)

This lesson covers the basics of date handling in C using struct tm, so you can build a date and find its day of the week. It's for anyone searching "C struct tm usage."

struct tm lets you build and work with a specific date. It's C's standard date representation, provided by the time.h header.

The example sets the year, month, and day into d.tm_year, d.tm_mon, and d.tm_mday, and calling the mktime() function automatically computes information like the day of the week. Notice the distinctly C-flavored quirk: the year is stored as years since 1900, and the month starts at 0.

A common early mistake is how tm_year and tm_mon are counted. You need to specify actual year - 1900 and actual month - 1 โ€” not knowing this produces the wrong date.

In real projects, handling dates โ€” displaying a post's timestamp, computing a deadline, and more โ€” is an essential part of nearly every application.

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

๐Ÿงช This site can't compile or run C 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)