T
Tech Town
Log InSign Up Free
← Back to Blog

June 19, 2025 · admin

Python Dates


๐Ÿ•’ Python datetime Module โ€“ Master Dates & Times in Python | TechTown.in

Working with dates and times is a common need in real-world applications โ€” from setting reminders and logging activity, to timestamping messages and managing bookings.

Python makes this easy with the powerful datetime module.

In this guide, youโ€™ll learn how to create, manipulate, and format dates and times using Python’s datetime module โ€” with simple examples and real-life use cases.


๐Ÿ“ฆ What is the datetime Module?

The datetime module in Python lets you work with date and time objects in a clean and readable way.

โœ… To get started:

import datetime

๐Ÿ“… Get the Current Date and Time

import datetime

now = datetime.datetime.now()
print(now)

๐ŸŽฏ Output:

2025-06-26 12:30:45.123456

โœ… This returns the current local date and time, down to microseconds.


๐Ÿงฑ Breakdown of datetime.now()

AttributeMeaningExample
yearYear2025
monthMonth (1โ€“12)6
dayDay of month26
hour24-hour format12
minuteMinute30
secondSecond45
print(now.year)
print(now.month)
print(now.day)

๐Ÿ“… Create a Custom Date

my_date = datetime.datetime(2023, 12, 31)
print(my_date)

โœ… You must specify: year, month, and day. Time is optional (defaults to 00:00:00).


โฐ Format Dates with strftime()

You can convert dates into readable strings using strftime():

now = datetime.datetime.now()
print(now.strftime("%A, %d %B %Y"))

๐ŸŽฏ Output:

Wednesday, 26 June 2025

Popular strftime() format codes:

CodeOutput ExampleDescription
%Y2025Full year
%m06Month (zero-padded)
%d26Day (zero-padded)
%H14Hour (24-hour)
%I02Hour (12-hour)
%pPMAM/PM
%AWednesdayDay name
%BJuneMonth name

๐Ÿ” Parse Strings to Dates with strptime()

date_string = "2025-06-26"
dt = datetime.datetime.strptime(date_string, "%Y-%m-%d")
print(dt)

๐ŸŽฏ Output:

2025-06-26 00:00:00

Use this when reading date strings from user input or files.


โž• Date Arithmetic with timedelta

from datetime import timedelta

today = datetime.datetime.now()
tomorrow = today + timedelta(days=1)
print(tomorrow)

โœ… You can add or subtract days, hours, minutes, etc.

one_week_later = today + timedelta(weeks=1)
three_hours_ago = today - timedelta(hours=3)

โฑ๏ธ Time Only โ€“ Use datetime.time

t = datetime.time(9, 45, 0)
print(t)

๐ŸŽฏ Output:

09:45:00

๐Ÿงช Real-Life Use Case โ€“ Expiry Reminder System

expiry_date = datetime.datetime(2025, 7, 1)
today = datetime.datetime.now()

if expiry_date > today:
    print("โœ… Still valid")
else:
    print("โš ๏ธ Expired!")

Use this logic in event scheduling, subscriptions, library systems, etc.


๐Ÿ“ Summary โ€“ Python datetime Cheatsheet

TaskCode Example
Current timedatetime.datetime.now()
Custom datedatetime.datetime(2023, 12, 31)
Format as string.strftime("%Y-%m-%d")
Convert string to datestrptime("2025-06-26", "%Y-%m-%d")
Add time+ timedelta(days=2)
Get just timedatetime.time(14, 30)

๐Ÿ Final Thoughts

The datetime module is a must-know if you’re building apps that involve:

  • Deadlines & timers
  • Logging
  • Timestamps in databases
  • Reminders and notifications

With just a few lines of code, Python lets you work with time like a pro.


๐Ÿ“˜ Continue learning powerful Python modules at TechTown.in