Python datetime Module

Python datetime Module

Kishore V


Python datetime Module

Python does not have a standalone date data type. Instead, it provides the powerful datetime module, which allows you to work with dates, times, and timestamps using dedicated objects.

To begin working with dates, you must first import the module.

Getting the Current Date and Time

You can retrieve the current local date and time using the now() method.

Example: Display the Current Timestamp

Output:

Current date and time: 2026-03-31 10:45:19.123456
Try it Yourself

This output includes:

    • Year

    • Month

    • Day

    • Hour

    • Minute

    • Second

    • Microsecond

Extracting Date Components

A datetime object provides easy access to individual date and time values.

Example: Get Year and Weekday

Output:

Year: 2026
Weekday: Tuesday
Try it Yourself

    • year returns the numeric year

    • strftime("%A") returns the full weekday name

Creating Custom Date Objects

You can create your own date and time values using the datetime() constructor.

Required Parameters

    • year

    • month

    • day

Optional Parameters

    • hour

    • minute

    • second

    • microsecond

    • Tzinfo

Example: Create a Specific Date

Output:

Event date: 2024-12-25 00:00:00
Try it Yourself

If time values are not provided, they default to 00:00:00.

Example: Create a Date with Time

Output:

Meeting scheduled at: 2025-03-10 14:30:00
Try it Yourself

Formatting Dates with strftime()

The strftime() method converts a datetime object into a human-readable string using format codes.

Example: Display Month Name

Output:

Month: November
Try it Yourself

Common strftime() Format Codes

Code Meaning Example
%aWeekday (short)Tue
%AWeekday (full)Tuesday
%dDay of month09
%bMonth (short)Nov
%BMonth (full)November
%mMonth number11
%yYear (2 digits)25
%YYear (4 digits)2025
%HHour (24-hour)18
%IHour (12-hour)06
%pAM / PMPM
%MMinute45
%SSecond30
%fMicrosecond123456
%jDay of the year320
%UWeek number (Sunday start)47
%WWeek number (Monday start)47
%cFull date & timeTue Nov 15 18:45:30 2025
%xLocal date11/15/25
%XLocal time18:45:30
%%Percent symbol%

Python strftime() Format Codes – Complete Examples

We’ll use this datetime object in all examples:


Weekday Formats

Output:

Tue
Tuesday
Try it Yourself

    • %a → Short weekday name

    • %A → Full weekday name

Day of Month

Output:

15
Try it Yourself

    • %d → Day of the month (01–31)

Month Formats

Output:

Nov
November
11
Try it Yourself

    • %b → Short month name

    • %B → Full month name

    • %m → Month number

Year Formats

Output:

25
2025
Try it Yourself

    • %y → Year (2 digits)

    • %Y → Year (4 digits)

Hour Formats

Output:

18
06
PM
Try it Yourself

    • %H → Hour (24-hour format)

    • %I → Hour (12-hour format)

    • %p → AM / PM

Minute, Second, Microsecond

Output:

45
30
123456
Try it Yourself

    • %M → Minutes

    • %S → Seconds

    • %f → Microseconds

Day & Week Information

Output:

319
46
46
Try it Yourself

    • %j → Day of the year

    • %U → Week number (Sunday as first day)

    • %W → Week number (Monday as first day)

Full Date & Time Formats

Output:

Tue Nov 15 18:45:30 2025
11/15/25
18:45:30
Try it Yourself

    • %c → Full date and time

    • %x → Local date

    • %X → Local time

Percent Symbol

Output:

%
Try it Yourself

    • %% → Prints a literal % symbol

Real-World Example (Bonus)

Output:

Tuesday, 15 November 2025 06:45 PM
Try it Yourself

Our website uses cookies to enhance your experience. Learn More
Accept !