Ad space (banner)
🐍Python Lessons
Lesson 37 / 69

Building and Displaying Dates (basic year/month/day operations)

This lesson covers the basics of date handling with Python's datetime module, so you can correctly build and display a year, month, day, and weekday. It's written for anyone searching "Python datetime tutorial".

datetime.date lets you construct and work with a specific date. Getting the weekday, or doing arithmetic between two dates, is straightforward. Unlike JavaScript, months are handled as plain numbers 1 through 12, which feels more intuitive. .weekday() returns the day of the week as a number from 0 (Monday) to 6 (Sunday).

The sample code creates a date object for August 9, 2026 with date(2026, 8, 9), and retrieves each part with the .year, .month, and .day attributes. Watch out that .weekday() starts counting from Monday as 0 — if you expect Sunday-first counting, you'll be off by one.

A common beginner mistake is the basis for counting weekdays. JavaScript's .getDay() starts from Sunday as 0, while Python's .weekday() starts from Monday as 0. The convention differs by language, so if you have experience with another language, pay extra attention here. For date/time handling that accounts for time zones, you'll reach for more advanced features.

This is essential knowledge for anything date-related, like a reservation system or an event calendar, and comes up constantly for calculating the difference between dates. Subtracting one date object from another gives you a timedelta representing elapsed days — a mechanism used constantly in real work.

Python
OUTPUT

💡 The Python engine may take a few seconds to load the first time you run code.

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