Getting just the first date of a month is quite simple - since it equals 1
all the time. You can even do this without needing the datetime
module to simplify calculations for you, if today_date
is always a string "Year-Month-Day"
(or any consistent format - parse it accordingly)
today_date = '2021-04-17'
y, m, d = today_date.split('-')
first_day_current = f"{y}-{m}-01"
y, m = int(y), int(m)
first_day_next = f"{y+(m==12)}-{m%12+1}-01"
If you want to use datetime.date()
, then you'll anyway have to convert the string to (year, month, date) int
s to give as arguments (or do today_date = datetime.date.today()
.
Then .replace(day=1)
to get first_day_current.
datetime.timedelta
can't add months (only upto weeks), so you'll need to use other libraries for this. But it's more imports and calculations to do the same thing in effect.