date formatting
print date
Python date
programming tutorial
date display

How to print a date in a regular format?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Printing a date in a "regular format" usually means choosing one clear string representation and applying it consistently. In Python, the right tool is usually datetime.strftime, but the real decision is which format you want to standardize on: local human-readable output or a globally unambiguous format such as ISO 8601.

Start With a datetime Object

Python formatting works best when you are dealing with real date or datetime objects instead of manual string slicing.

python
1from datetime import datetime
2
3now = datetime.now()
4print(now)

The default printed form is useful for debugging, but it is not always the best choice for user-facing output.

Use strftime for Explicit Formatting

strftime lets you choose the exact output pattern.

python
1from datetime import datetime
2
3now = datetime(2026, 3, 7, 14, 30, 45)
4print(now.strftime("%Y-%m-%d"))
5print(now.strftime("%d/%m/%Y"))
6print(now.strftime("%m/%d/%Y"))

Common directives include:

  • '%Y for four-digit year.'
  • '%m for month.'
  • '%d for day.'
  • '%H for hour in 24-hour format.'
  • '%M for minute.'
  • '%S for second.'

Once you pick a format string, every date printed with it becomes consistent.

Choose the Format Based on Audience

A "regular" format depends on context.

For user interfaces in one region, a localized style may be reasonable:

python
print(now.strftime("%d/%m/%Y"))

For logs, filenames, APIs, and data exchange, an ISO-like style is usually better:

python
print(now.strftime("%Y-%m-%d"))
print(now.strftime("%Y-%m-%d %H:%M:%S"))

The advantage of year-month-day is that it sorts naturally and avoids month-day ambiguity.

Formatting a date Without Time

If you only need the date portion, use date.today() or a date object directly.

python
1from datetime import date
2
3today = date.today()
4print(today.strftime("%Y-%m-%d"))

This is cleaner than formatting a full timestamp and then ignoring the time component.

ISO 8601 Is Often the Best Default

Python also supports ISO-style output without a custom format string.

python
1from datetime import datetime
2
3now = datetime(2026, 3, 7, 14, 30, 45)
4print(now.date().isoformat())
5print(now.isoformat())

This is a strong default when you care about machine readability, APIs, or stable logging formats.

Parse First, Then Reformat

If you start with a date string in one format and want to print it in another, parse it first with strptime, then output it with strftime.

python
1from datetime import datetime
2
3raw = "07/03/2026"
4parsed = datetime.strptime(raw, "%d/%m/%Y")
5print(parsed.strftime("%Y-%m-%d"))

This is much safer than manually splitting strings because the parser validates the structure for you.

Keep Storage and Display Separate

A good rule is to store dates in a stable machine-friendly representation and format them only at the display boundary. That prevents presentation choices from leaking into persistence or API contracts.

Locale-sensitive month and weekday names deserve extra care if the output is meant for users in multiple regions.

Common Pitfalls

  • Calling something a "regular format" without defining which actual format the project should use.
  • Mixing DD/MM/YYYY and MM/DD/YYYY in the same application.
  • Storing dates as display strings instead of real date objects.
  • Manually slicing strings instead of using strptime and strftime.
  • Using local display formats in logs or APIs where unambiguous ISO-style output would be safer.

Summary

  • In Python, strftime is the standard tool for printing dates in a chosen format.
  • Pick one format deliberately instead of letting defaults drift across the codebase.
  • Use date objects when you only need a date and not a timestamp.
  • Prefer ISO-style output for logs, APIs, and data interchange.
  • Parse strings with strptime before reformatting them into a cleaner display format.

Course illustration
Course illustration

All Rights Reserved.