A Date With the Datetime Module in Python | Part - 2

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.

  1. isocalendar()

  2. 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.

DirectiveMeaningExample
%aWeekday in short-form.Sun, Mon
%AWeekday in full-form.Sunday, Monday
%wWeekday as number. (Starts from 0 as Sunday)0, 1, …, 6
%dDay of the month as a zero-padded decimal number.01, 02, …, 31
%bMonth in short-formJan, Feb
%BMonth in full-formJanuary, February
%mMonth as a zero-padded decimal number.01, 02, …, 12
%yYear without century as a zero-padded decimal number. 22 for 2022.00, 01, …, 99
%YYear with century as a decimal number.0001, 0002, …, 2013, 2014, …, 9998, 9999
%HHour (24-hour clock) as a zero-padded decimal number.00, 01, …, 23
%IHour (12-hour clock) as a zero-padded decimal number.01, 02, …, 12
%pLocale’s equivalent of either AM or PM.AM, PM (en_US); am, pm (de_DE)
%MMinute as a zero-padded decimal number.00, 01, …, 59
%SSecond as a zero-padded decimal number.00, 01, …, 59
%fMicrosecond as a decimal number, zero-padded to 6 digits.000000, 000001, …, 999999
%zUTC offset in the form ±HHMM[SS[.ffffff]] (empty string if the object is naive).(empty), +0000, -0400, +1030, +063415, -030712.345216
%ZTime zone name (empty string if the object is naive).(empty), UTC, GMT
%jDay of the year as a zero-padded decimal number.001, 002, …, 366
%UWeek 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
%WWeek 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
%cLocale’s appropriate date and time representation.Tue Aug 16 21:30:00 1988 (en_US); Di 16 Aug 21:30:00 1988 (de_DE)
%xLocale’s appropriate date representation.08/16/88 (None); 08/16/1988 (en_US); 16.08.1988 (de_DE)
%XLocale’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
"""