Sitemap
Datawhiz

Datawhiz

Date Time

2 min readDec 15, 2025

Python Chapter VI

Essential datetime module

Get Datawhiz’s stories in your inbox

Join Medium for free to get updates from this writer.

import datetime as dt #or from datetime import date - it allow you don't use dt.date instead only use date
#create a date object
mydate=dt.date(year=2001,month=8,day=8)
print(f"Date is: {mydate}")
# get the current d
ate
print(dt.date.today())
# get the curren month
print(dt.date.today().month)
# Get the current year
print(dt.date.today().year)
# Get the current day
print(dt.date.today().day)
print(dt.datetime.now())

from datetime import time,timedelta,datetime
# Create a time object with the specified hour, minute, and second
mytime = time(hour=12, minute=30, second=45)
# Display the time object
print(mytime)

# Create a time object with the specified minute
mytime = time(minute=15)

# Display the time object
print(mytime)

# Access the components of the time object separately
# Display the hour
print(mytime.hour)

# Display the Minute
print(mytime.minute)

# Display the Second
print(mytime.second)

# Get the current date and time
todays_date = datetime.now()

# Calculate the date after adding 90 days
dateafter_months = todays_date + timedelta(days=90)

# Display the resulting date
print(dateafter_months)

# Calculate the total number of seconds in a month (30 days)
seconds_in_a_month = timedelta(days=30).total_seconds()

# Display the result
print("Total seconds in a month:", seconds_in_a_month)

date_str = "14.12.2025"

dt = datetime.strptime(date_str, "%d.%m.%Y")
day_name = dt.strftime("%A")

print(day_name)
#output
Date is: 2001-08-08
2025-12-14
12
2025
14
2025-12-14 21:26:35.364372
12:30:45
00:15:00
0
15
0
2026-03-14 21:26:35.364372
Total seconds in a month: 2592000.0
sunday
Press enter or click to view image in full size

--

--