🎓 All Courses | 📚 Python Programming Syllabus

📋 Study this course on TaskLoco
#python-programming#datetime#dates

Dates & Times with datetime

from datetime import datetime, date, timedelta

# Current time
now = datetime.now()
today = date.today()

print(now.year, now.month, now.day)
print(now.hour, now.minute, now.second)

Creating dates:

birthday = date(1990, 6, 15)
event = datetime(2024, 12, 25, 9, 0, 0)

Arithmetic:

one_week = timedelta(days=7)
next_week = today + one_week
yesterday = today - timedelta(days=1)

diff = date(2025, 1, 1) - today
print(diff.days)  # days until New Year

Formatting & parsing:

formatted = now.strftime("%Y-%m-%d %H:%M:%S")
# "2024-03-15 14:30:00"

parsed = datetime.strptime("2024-03-15", "%Y-%m-%d")

Common format codes: %Y=year, %m=month, %d=day, %H=hour(24), %I=hour(12), %M=minute, %S=second, %A=weekday name.


YouTube • Top 10
Python Programming: Working with Dates & Times
Tap to Watch ›
📸
Google Images • Top 10
Python Programming: Working with Dates & Times
Tap to View ›

Reference:

Wikipedia: ISO 8601

image for linkhttps://en.wikipedia.org/wiki/ISO_8601

📚 Python Programming — Full Course Syllabus
📋 Study this course on TaskLoco

TaskLoco™ — The Sticky Note GOAT