
Python Datetime¶
- Python does not include a dedicated data type for dates. However, it provides a built-in module called
datetime
, which allows us to create and manipulate date and time as objects.
Importing datetime
and Getting Current Date-Time¶
- The
datetime
module offers a class calleddatetime
which provides methods like.now()
to access the current timestamp.
In [2]:
# Import the datetime module and display the current date:
import datetime
# Get the current date and time
current_time = datetime.datetime.now()
print("Timestamp :", current_time)
# Output: Timestamp : 2025-06-17 18:59:17.181200
Timestamp : 2025-06-17 18:59:17.181200
Extracting Date Components¶
- You can extract specific parts like year, month, day, hour, minute, second, and microsecond.
In [6]:
import datetime
# Get current date and time
now = datetime.datetime.now()
# Extract year and weekday name
print("Year:", now.year)
print("Weekday :", now.strftime("%A"))
# Output:
# Year: 2025
# Weekday: Tuesday
Year: 2025 Weekday : Tuesday
Creating Custom Date Objects¶
- To define a specific date, you can instantiate a
datetime
object manually.
In [8]:
import datetime
# Create a specific date
release_date = datetime.datetime(2023, 1, 15)
print("Release Date:", release_date)
# Output: Release Date: 2023-01-15 00:00:00
Release Date: 2023-01-15 00:00:00
Formatting Date Strings with strftime()¶
- The
strftime()
method formats datetime objects into readable strings using format codes.
In [10]:
import datetime
# Create a datetime object
x = datetime.datetime(2024, 10, 30)
# Format the month name (full)
print("Month:", x.strftime("%B"))
# Output: Month: October
Month: October
All Format Codes Reference¶
- Use these format codes with
.strftime()
to display dates/times in customized ways:
Code | Description | Example Output |
---|---|---|
%a |
Weekday, short | Wed |
%A |
Weekday, full | Wednesday |
%w |
Weekday (0-6, Sunday=0) | 3 |
%d |
Day of month | 07 |
%b |
Month, short | Dec |
%B |
Month, full | December |
%m |
Month as number | 12 |
%y |
Year (2 digits) | 25 |
%Y |
Year (4 digits) | 2025 |
%H |
Hour (24h) | 16 |
%I |
Hour (12h) | 04 |
%p |
AM/PM | PM |
%M |
Minutes | 59 |
%S |
Seconds | 08 |
%f |
Microseconds | 548513 |
%z |
UTC Offset | +0530 |
%Z |
Timezone name | IST |
%j |
Day number of the year | 123 |
%U |
Week number (Sunday start) | 25 |
%W |
Week number (Monday start) | 24 |
%c |
Local date & time | Tue Jun 17 12:45:33 2025 |
%C |
Century | 20 |
%x |
Local date | 06/17/25 |
%X |
Local time | 12:45:33 |
%% |
Literal percent | % |
%G |
ISO year | 2025 |
%u |
ISO weekday (1=Mon) | 2 |
%V |
ISO week number | 25 |
In [13]:
# Example: Using Multiple Format Codes
import datetime
# Current datetime
now = datetime.datetime.now()
# Display formatted date and time parts
print("Short weekday:", now.strftime("%a"))
print("Full weekday:", now.strftime("%A"))
print("Weekday as number:", now.strftime("%w"))
print("Day of the month:", now.strftime("%d"))
print("Short month:", now.strftime("%b"))
print("Full month:", now.strftime("%B"))
print("Month as number:", now.strftime("%m"))
print("Short year:", now.strftime("%y"))
print("Full year:", now.strftime("%Y"))
print("Hour (24h):", now.strftime("%H"))
print("Hour (12h):", now.strftime("%I"))
print("AM/PM:", now.strftime("%p"))
print("Minute:", now.strftime("%M"))
print("Second:", now.strftime("%S"))
print("Microsecond:", now.strftime("%f"))
print("Day of year:", now.strftime("%j"))
print("Week # (Sun-start):", now.strftime("%U"))
print("Week # (Mon-start):", now.strftime("%W"))
print("Local date & time:", now.strftime("%c"))
print("Century:", now.strftime("%C"))
print("Local date:", now.strftime("%x"))
print("Local time:", now.strftime("%X"))
print("Literal percent:", now.strftime("%%"))
print("ISO year:", now.strftime("%G"))
print("ISO weekday:", now.strftime("%u"))
print("ISO week number:", now.strftime("%V"))
# Sample Output: Values will differ based on current datetime.
Short weekday: Tue Full weekday: Tuesday Weekday as number: 2 Day of the month: 17 Short month: Jun Full month: June Month as number: 06 Short year: 25 Full year: 2025 Hour (24h): 19 Hour (12h): 07 AM/PM: PM Minute: 20 Second: 32 Microsecond: 229355 Day of year: 168 Week # (Sun-start): 24 Week # (Mon-start): 24 Local date & time: Tue Jun 17 19:20:32 2025 Century: 20 Local date: 06/17/25 Local time: 19:20:32 Literal percent: % ISO year: 2025 ISO weekday: 2 ISO week number: 25