Mastering Date & Time Handling in Python

🐍 𝐃𝐚𝐲 𝟖 (𝐄𝐯𝐞𝐧𝐢𝐧𝐠) 𝐨𝐟 𝐌𝐲 𝟏𝟓-𝐃𝐚𝐲 𝐏𝐲𝐭𝐡𝐨𝐧 𝐂𝐡𝐚𝐥𝐥𝐞𝐧𝐠𝐞 — 𝐃𝐚𝐭𝐞 & 𝐓𝐢𝐦𝐞 𝐇𝐚𝐧𝐝𝐥𝐢𝐧𝐠 In today’s evening session, I focused on date and time handling — a critical skill for logging, analytics, scheduling, and reporting. Python’s datetime module makes working with dates and times straightforward. 🔹 𝐖𝐡𝐚𝐭 𝐈 𝐂𝐨𝐯𝐞𝐫𝐞𝐝 𝐓𝐨𝐝𝐚𝐲 ✅ Getting Current Date & Time from datetime import datetime now = datetime.now() print(now) ✅ Working with Dates from datetime import date today = date.today() print(today) ✅ Formatting Dates formatted = now.strftime("%Y-%m-%d %H:%M:%S") print(formatted) ✅ Date Arithmetic with timedelta from datetime import timedelta future_date = today + timedelta(days=7) print(future_date) ✅ Difference Between Dates start_date = date(2024, 1, 1) end_date = date.today() difference = end_date - start_date print(difference.days) 🎯 𝐊𝐞𝐲 𝐓𝐚𝐤𝐞𝐚𝐰𝐚𝐲 Date and time handling is essential for real-world applications like reports, logs, and automation. Mastering datetime and timedelta makes time-based logic much easier. ✅ Day 8 Completed 𝐓𝐨𝐦𝐨𝐫𝐫𝐨𝐰: 𝐃𝐚𝐲 𝟗 (𝐌𝐨𝐫𝐧𝐢𝐧𝐠) — 𝐎𝐎𝐏 𝐁𝐚𝐬𝐢𝐜𝐬 (𝐂𝐥𝐚𝐬𝐬𝐞𝐬 & 𝐎𝐛𝐣𝐞𝐜𝐭𝐬) Let’s keep moving #Python #DateTime #15DaysOfPython #LearningInPublic #Programming

To view or add a comment, sign in

Explore content categories