Advanced

Dates

Use Python’s datetime and zoneinfo modules to create, format, parse, and manipulate dates and times efficiently.

Working with Dates and Times

Using the datetime Module

Python’s datetime module provides classes to handle dates and times.

pythonCopyEditfrom datetime import datetime, date, time

# Current date and time
now = datetime.now()
print(now)

# Creating specific dates and times
birthday = date(1990, 5, 17)
meeting_time = time(14, 30)

print(birthday, meeting_time)

Formatting Dates and Times

Format date/time as strings using strftime:

pythonCopyEditnow = datetime.now()
formatted = now.strftime("%Y-%m-%d %H:%M:%S")
print(formatted)  # Example: 2025-07-07 12:30:45

Parsing Strings to Dates

Convert strings to datetime objects with strptime:

pythonCopyEditdate_str = "2025-07-07 12:30:45"
dt = datetime.strptime(date_str, "%Y-%m-%d %H:%M:%S")
print(dt)

Handling Timezones

Use the zoneinfo module (Python 3.9+):

pythonCopyEditfrom datetime import datetime
from zoneinfo import ZoneInfo

now_utc = datetime.now(tz=ZoneInfo("UTC"))
now_ny = now_utc.astimezone(ZoneInfo("America/New_York"))

print(now_utc)
print(now_ny)

Working with Dates and Times

Using the datetime Module

Python’s datetime module provides classes to handle dates and times.

pythonCopyEditfrom datetime import datetime, date, time

# Current date and time
now = datetime.now()
print(now)

# Creating specific dates and times
birthday = date(1990, 5, 17)
meeting_time = time(14, 30)

print(birthday, meeting_time)

Formatting Dates and Times

Format date/time as strings using strftime:

pythonCopyEditnow = datetime.now()
formatted = now.strftime("%Y-%m-%d %H:%M:%S")
print(formatted)  # Example: 2025-07-07 12:30:45

Parsing Strings to Dates

Convert strings to datetime objects with strptime:

pythonCopyEditdate_str = "2025-07-07 12:30:45"
dt = datetime.strptime(date_str, "%Y-%m-%d %H:%M:%S")
print(dt)

Handling Timezones

Use the zoneinfo module (Python 3.9+):

pythonCopyEditfrom datetime import datetime
from zoneinfo import ZoneInfo

now_utc = datetime.now(tz=ZoneInfo("UTC"))
now_ny = now_utc.astimezone(ZoneInfo("America/New_York"))

print(now_utc)
print(now_ny)

Working with Dates and Times

Using the datetime Module

Python’s datetime module provides classes to handle dates and times.

pythonCopyEditfrom datetime import datetime, date, time

# Current date and time
now = datetime.now()
print(now)

# Creating specific dates and times
birthday = date(1990, 5, 17)
meeting_time = time(14, 30)

print(birthday, meeting_time)

Formatting Dates and Times

Format date/time as strings using strftime:

pythonCopyEditnow = datetime.now()
formatted = now.strftime("%Y-%m-%d %H:%M:%S")
print(formatted)  # Example: 2025-07-07 12:30:45

Parsing Strings to Dates

Convert strings to datetime objects with strptime:

pythonCopyEditdate_str = "2025-07-07 12:30:45"
dt = datetime.strptime(date_str, "%Y-%m-%d %H:%M:%S")
print(dt)

Handling Timezones

Use the zoneinfo module (Python 3.9+):

pythonCopyEditfrom datetime import datetime
from zoneinfo import ZoneInfo

now_utc = datetime.now(tz=ZoneInfo("UTC"))
now_ny = now_utc.astimezone(ZoneInfo("America/New_York"))

print(now_utc)
print(now_ny)

© Layer Docs

Made by Alejandro · Powered by Framer

Create a free website with Framer, the website builder loved by startups, designers and agencies.