finding first day of the month in python
Can be done on the same line using date.replace
:
from datetime import datetime
datetime.today().replace(day=1)
This is a pithy solution.
import datetime
todayDate = datetime.date.today()
if todayDate.day > 25:
todayDate += datetime.timedelta(7)
print todayDate.replace(day=1)
One thing to note with the original code example is that using timedelta(30)
will cause trouble if you are testing the last day of January. That is why I am using a 7-day delta.