How to Get the Current Date in Python

What you’ll build or solve

You’ll get today’s date in Python and format it for logs, filenames, and user-facing text.

When this approach works best

This approach works best when you:

  • Add today’s date to a report name like report-2026-02-17.csv.
  • Log events with the current date, like “backup ran on 2026-02-17”.
  • Compare dates, like checking if a subscription expires today.

Avoid using a date-only value when you need an exact moment in time. For timestamps, use datetime.now() and include the time and timezone.

Prerequisites

  • Python installed
  • You know what a variable is

Step-by-step instructions

1) Get today’s date as a date object

Use date.today() when you only need the date (year, month, day).

fromdatetimeimportdate

today=date.today()
print(today)

What to look for:

today is a date object, not a string. You can compare it to other date values and access parts like today.year.


2) Format the date for display or filenames

Use .isoformat() for a safe default (YYYY-MM-DD). Use strftime() when you need a custom format.

fromdatetimeimportdate

today=date.today()

print(today.isoformat())# 2026-02-17
print(today.strftime("%d.%m.%Y"))# 17.02.2026

Option A: Build a filename-friendly date string

fromdatetimeimportdate

stamp=date.today().isoformat()
filename=f"backup-{stamp}.zip"
print(filename)

What to look for:

strftime() uses format codes like %Y (year), %m (month), %d (day). Keep the output free of / if you plan to use it in filenames.


3) Get the date in a specific timezone

Your computer’s local timezone controls what “today” means. If your code runs on a server in another region, set the timezone explicitly.

fromdatetimeimportdatetime
fromzoneinfoimportZoneInfo

now_in_podgorica=datetime.now(ZoneInfo("Europe/Podgorica"))
today_in_podgorica=now_in_podgorica.date()

print(today_in_podgorica)

What to look for:

zoneinfo is built into modern Python. If you get an import error, upgrade to Python 3.9+.


Examples you can copy

Example 1: Print today’s date and the weekday

fromdatetimeimportdate

today=date.today()
print(today)
print(today.strftime("%A"))# Tuesday, Wednesday, etc.

Example 2: Create a daily report filename

fromdatetimeimportdate

report_name=f"sales-{date.today().isoformat()}.csv"
print(report_name)

Example 3: Check if a deadline is today

fromdatetimeimportdate

deadline=date(2026,2,17)

ifdate.today()==deadline:
print("Deadline is today.")
else:
print("Not today.")

Example 4: Parse a date string and compare it to today

fromdatetimeimportdate

expires=date.fromisoformat("2026-03-01")

ifexpires<date.today():
print("Expired.")
else:
print("Active.")

Example 5: Display a date in a UI-friendly format

fromdatetimeimportdate

label=date.today().strftime("%b %d, %Y")# Feb 17, 2026
print(label)

Common mistakes and how to fix them

Mistake 1: Using datetime.now() when you only need the date

What you might do

fromdatetimeimportdatetime

today=datetime.now()
print(today)

Why it breaks

You get a full timestamp, not just the date. Comparisons to a date value will also fail unless you convert types.

Fix

Use date.today() or call .date() on a timezone-aware datetime.

fromdatetimeimportdate

today=date.today()
print(today)

Mistake 2: Confusing strftime() and strptime()

What you might do

fromdatetimeimportdate

# Trying to parse a string using strftime (wrong direction)
d=date.strftime("2026-02-17","%Y-%m-%d")

Why it breaks

strftime() formats a date into a string. It does not parse strings.

Fix

Use date.fromisoformat() for YYYY-MM-DD, or datetime.strptime() for custom formats.

fromdatetimeimportdate,datetime

d1=date.fromisoformat("2026-02-17")
d2=datetime.strptime("17.02.2026","%d.%m.%Y").date()

print(d1,d2)

Troubleshooting

If you see ImportError: cannot import name 'ZoneInfo', upgrade to Python 3.9+ or skip timezone support and use date.today() in local time.

If today’s date looks “wrong” on a server, the server's timezone may differ. Use datetime.now(ZoneInfo("Your/Timezone")).date().

If your formatted date includes slashes, avoid them for filenames. Use - or _ in strftime(), like %Y-%m-%d.

If parsing fails with ValueError, your input string does not match the expected format. Print the raw string and confirm the pattern you want to parse.


Quick recap

  • Use date.today() to get the current date as a date object.
  • Use .isoformat() for YYYY-MM-DD, or strftime() for custom formats.
  • Use ZoneInfo with datetime.now(...) when timezone matters.
  • Use date.fromisoformat(...) or datetime.strptime(...) to parse date strings.