date, time, datetime
Create and read date objects.
date, time, datetime is a free Python 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 Python Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Meet the datetime Module
Python's datetime module lives in the standard library and gives you tools to work with calendar dates and clock times.
datestores a year, month, and day.timestores hours, minutes, seconds, and microseconds.datetimecombines both into one object.
Import it with from datetime import date, time, datetime.
from datetime import date, time, datetime
print(date)
print(time)
print(datetime)Creating a date
Build a date by passing year, month, day as integers. Months and days are 1-based and validated automatically.
from datetime import date
birthday = date(1995, 7, 14)
print(birthday)Reading date fields
Each part of a date is available as an attribute: .year, .month, and .day. These are plain integers you can use in calculations.
from datetime import date
d = date(2026, 5, 30)
print('Year:', d.year)
print('Month:', d.month)
print('Day:', d.day)Today's date
Use date.today() to get the current local date. It returns a date object with no time component.
from datetime import date
today = date.today()
print('Today is', today)
print('It is the year', today.year)Creating a time
A time object represents a moment on the clock. Pass hour, minute, second (all optional, default 0). Hours run 0 to 23.
from datetime import time
lunch = time(12, 30, 0)
print(lunch)
print('Hour:', lunch.hour)
print('Minute:', lunch.minute)Creating a datetime
A datetime fuses date and time. Pass year, month, day, hour, minute, second. The time parts default to zero if omitted.
from datetime import datetime
launch = datetime(2026, 5, 30, 9, 15, 0)
print(launch)
print('Day:', launch.day)
print('Hour:', launch.hour)The current moment
datetime.now() returns the current local date and time. This is the most common way to timestamp events.
from datetime import datetime
now = datetime.now()
print('Now:', now)
print('Just the hour:', now.hour)Splitting a datetime apart
A datetime can hand back its pieces. Call .date() for the date portion and .time() for the time portion.
from datetime import datetime
moment = datetime(2026, 5, 30, 14, 45, 30)
print('Date part:', moment.date())
print('Time part:', moment.time())Combining date and time
If you have separate date and time objects, datetime.combine() merges them into one datetime.
from datetime import date, time, datetime
d = date(2026, 5, 30)
t = time(8, 0)
combined = datetime.combine(d, t)
print(combined)Comparing dates
Date and datetime objects support comparison operators like <, >, and ==. Earlier dates are considered smaller.
from datetime import date
a = date(2026, 1, 1)
b = date(2026, 12, 31)
print(a < b)
print(a == b)
print(max(a, b))Weekday of a date
.weekday() returns the day of week as an integer where Monday is 0 and Sunday is 6. .isoweekday() uses 1 to 7 instead.
from datetime import date
d = date(2026, 5, 30)
print('weekday():', d.weekday())
print('isoweekday():', d.isoweekday())Quick Check
Which expression returns the current local date and time together?
Recap: Dates and Times
You now know the three core types:
date(y, m, d)for calendar dates, with.year,.month,.day.time(h, m, s)for clock times.datetime(...)combining both, plus.date()and.time()to split it.
Use date.today() and datetime.now() for the current moment, and compare objects directly with < and >.
from datetime import date, datetime
print('Today:', date.today())
print('Now:', datetime.now())Frequently asked questions
Is the “date, time, datetime” lesson free?
Yes — the full text of “date, time, datetime” is free to read here on the web, and the Python 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 Python Academy course, upgrade to CoddyKit PRO.
What will I learn in “date, time, datetime”?
Create and read date objects. You practise Python 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 Python Academy?
No prior experience is required. Python 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 “date, time, datetime” 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 Python Academy lesson?
Yes. Every Python 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
- date, time, datetime
- Timedelta and Arithmetic
- Formatting and Parsing
- Time Zones with zoneinfo