79687107

Date: 2025-07-02 08:44:57
Score: 1
Natty:
Report link

Based on @Cory 's reply I made a function that returns with the same date from the previous month. You just need datetime library:

from datetime import datetime, timedelta

def same_dt_in_prev_month(dt: datetime) -> datetime:
    orig_day = dt.day
    return (dt - timedelta(days=orig_day)).replace(day=orig_day)

one_month_ago = same_dt_in_prev_month(datetime.now())
print(one_month_ago)
Reasons:
  • Has code block (-0.5):
  • User mentioned (1): @Cory
  • Low reputation (0.5):
Posted by: andexte