A Date With the Datetime Module in Python | Part - 2
We have explored how to get current date, month, year and day of the week in the previous blog. In this blog post we will explore more of a datetime and its corresponding methods with some tutorial.
Get the Current Week Number using Python
There are two ways to get current number of week using datetime
.
isocalendar()
strftime()
# isocalendar()
from datetime import datetime
today_iso = datetime.today().isocalendar()
print("ISO Calendar:", today_iso)
print("Current Week:", today_iso.week)
"""
OUTPUT:
ISO Calendar: datetime.IsoCalendarDate(year=2022, week=43, weekday=2)
Current Week: 43
"""
As we can see, isocalender()
method can provides year, week, weekday. We can extract all of them in similar way we got week.
# strftime()
from datetime import datetime
today_iso = datetime.today().strftime("%V")
print("Current Week:", today_iso)
"""
OUTPUT:
Current Week: 43
"""
There are so many possibilities with strftime() method. Let's see what we cna do with strftime().
strftime() in Python
The strftime()
method helps us to represent date in appropriate string format. We can use the any of the following format codes as standalone or in combination. Syntax: strftime(format)
Note: The strftime() method can be used with and datetime, date and time objects.
Directive | Meaning | Example |
---|---|---|
%a | Weekday in short-form. | Sun, Mon |
%A | Weekday in full-form. | Sunday, Monday |
%w | Weekday as number. (Starts from 0 as Sunday) | 0, 1, …, 6 |
%d | Day of the month as a zero-padded decimal number. | 01, 02, …, 31 |
%b | Month in short-form | Jan, Feb |
%B | Month in full-form | January, February |
%m | Month as a zero-padded decimal number. | 01, 02, …, 12 |
%y | Year without century as a zero-padded decimal number. 22 for 2022. | 00, 01, …, 99 |
%Y | Year with century as a decimal number. | 0001, 0002, …, 2013, 2014, …, 9998, 9999 |
%H | Hour (24-hour clock) as a zero-padded decimal number. | 00, 01, …, 23 |
%I | Hour (12-hour clock) as a zero-padded decimal number. | 01, 02, …, 12 |
%p | Locale’s equivalent of either AM or PM. | AM, PM (en_US); am, pm (de_DE) |
%M | Minute as a zero-padded decimal number. | 00, 01, …, 59 |
%S | Second as a zero-padded decimal number. | 00, 01, …, 59 |
%f | Microsecond as a decimal number, zero-padded to 6 digits. | 000000, 000001, …, 999999 |
%z | UTC offset in the form ±HHMM[SS[.ffffff]] (empty string if the object is naive). | (empty), +0000, -0400, +1030, +063415, -030712.345216 |
%Z | Time zone name (empty string if the object is naive). | (empty), UTC, GMT |
%j | Day of the year as a zero-padded decimal number. | 001, 002, …, 366 |
%U | Week number of the year (Sunday as the first day of the week) as a zero-padded decimal number. All days in a new year preceding the first Sunday are considered to be in week 0. | 00, 01, …, 53 |
%W | Week number of the year (Monday as the first day of the week) as a zero-padded decimal number. All days in a new year preceding the first Monday are considered to be in week 0. | 00, 01, …, 53 |
%c | Locale’s appropriate date and time representation. | Tue Aug 16 21:30:00 1988 (en_US); Di 16 Aug 21:30:00 1988 (de_DE) |
%x | Locale’s appropriate date representation. | 08/16/88 (None); 08/16/1988 (en_US); 16.08.1988 (de_DE) |
%X | Locale’s appropriate time representation. | 21:30:00 (en_US); 21:30:00 (de_DE) |
How to Get Name of Month in Python
To get name of the month, we can use strftime
method with datetime
module.
# Month Name using Python
from datetime import datetime
today = datetime.today()
month_full = today.strftime("%B")
month_short = today.strftime("%b")
print("Today:", today)
print("Month in Full:", month_full)
print("Month in Short:", month_short)
"""
OUTPUT:
Today: 2022-10-30 16:56:49.644084
Month in Full: October
Month in Short: Oct
"""
How to Get Name of Weekday in Python
To get name of the week-day, we can use strftime
method with datetime
module.
# Week-Day Name using Python
from datetime import datetime
today = datetime.today()
day_full = today.strftime("%A")
day_short = today.strftime("%a")
print("Today:", today)
print("Day in Full:", day_full)
print("Day in Short:", day_short)
"""
OUTPUT:
Today: 2022-10-30 17:01:19.545097
Day in Full: Sunday
Day in Short: Sun
"""
Combine Multiple Format Codes in strftime() Method
So how to show date in a specific format. We can use any special character to separate format codes. To use % we have to write %%.
# Custom Date
from datetime import datetime
today = datetime.today()
custom_date = today.strftime("%d %B, %Y")
print("Today:", today)
print("Custom Date:", custom_date)
"""
OUTPUT:
Today: 2022-10-30 17:14:15.280584
Custom Date: 30 October, 2022
"""