python get the date for the first day of the current month in python

You can get the date for the first day of the current month in Python by using the datetime module:

main.py
import datetime

today = datetime.date.today()
first_day = today.replace(day=1)

print(first_day)
98 chars
7 lines

This code first imports the datetime module. It then gets today's date using the date.today() method. Finally, it replaces the day attribute of the current date with 1 to get the first day of the current month. The output will be in the YYYY-mm-dd format (e.g. 2021-01-01).

gistlibby LogSnag