Date, timezone, calendar

Datetime module supplies classes for maintain dates and times. While date and time arithmetic is supported, the focus of the implementation is on efficient attribute extraction for output formatting and manipulation.Date and time objects may be categorized as “aware” or “naive” depending on whether or not they include time zone information.

#date

from datetime import date
date.fromisoformat('2024-03-31')
#2024-03-31
date.fromisoformat('20241104')
#2024-11-04
date.fromisoformat('2024-W51-1')
#2024-12-16

print(date(2024, 12, 29).isocalendar())
#datetime.IsoCalendarDate(year=2024, week=52, weekday=7)

print(date(2024, 12, 10).ctime())
#Tue Dec 10 00:00:00 2024

d = date(2024, 12, 31)
d.replace(day=26)

#datetime

n = datetime.datetime.now()
#2024-11-29 23:06:32.839128
print(n)
print(n.year)
print(n.month)
print(n.day)
print(n.microsecond)
print(n.strftime("%A"))
#Friday
print(n.strftime("%B"))
#November
print("Week no.: " + n.strftime("%W"))
#Week no.: 48
print(n.strftime("%X"))
#23:06:32

obj = datetime.datetime(2024, 11, 29,23,26,16)
print(obj)

print(obj.strftime("%a %m %y"))
#Fri 11 24

print(obj.strftime("%m-%d-%Y %T:%M%p"))
#11-29-2024 23:26:16:26PM

#datetime

rom datetime import timedelta
duration = timedelta(seconds=11122233)
duration.days, duration.seconds

print(duration.total_seconds())

delta = timedelta(
    days=50,
    seconds=27,
    microseconds=10,
    milliseconds=29000,
    minutes=5,
    hours=8,
    weeks=2
)
# Only days, seconds, and microseconds remain
print(delta)

Pytz enables time-zone calculations in our Python applications and also allows us to create timezone aware datetime instances.

#zoneinfo

from datetime import datetime as dt
from pytz import timezone as tz

format = "%Y-%m-%d %H:%M:%S %Z%z"

# Current time in UTC
now_utc = dt.now(tz('UTC'))
print(now_utc.strftime(format))

# Convert to selected time zone
lagos_now = now_utc.astimezone(tz('Africa/Lagos'))
print(lagos_now.strftime(format))

barbados_now = now_utc.astimezone(tz('America/Barbados'))
print(barbados_now.strftime(format))

sydney_now = now_utc.astimezone(tz('Australia/Sydney'))
print(sydney_now.strftime(format))

michigan_now = now_utc.astimezone(tz('US/Michigan'))
print(michigan_now.strftime(format))

poland_now = now_utc.astimezone(tz('Poland'))
print(poland_now.strftime(format))

Calendar module allows to output calendars like the Unix cal program, and provides additional useful functions related to the calendar. Functions and classes defined in this module use an idealized calendar, the current Gregorian calendar extended indefinitely in both directions.

import calendar

print(list(calendar.day_name))
#['Monday', 'Tuesday', 'Wednesday',
# 'Thursday', 'Friday', 'Saturday',
# 'Sunday']
print(list(calendar.day_abbr))
#['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
print(list(calendar.month_abbr))
#['', 'Jan', 'Feb', 'Mar', 'Apr',
# 'May', 'Jun', 'Jul', 'Aug', 'Sep',
# 'Oct', 'Nov', 'Dec']

print full calendar from terminal level

python -m calendar 2025