Doing the following calculation to take 30 days of the current date using the date-time module. The calculation is correct but it's not trimming milliseconds to 3 digits. any idea how I can implement it?
from datetime import datetime,timedelta
datetime_limit = datetime.today().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]
datetime_limit = datetime.today() - timedelta(days=30)
CodePudding user response:
This may accomplish what you are trying to achieve:
from datetime import datetime, timedelta
datetime_limit = datetime.today()
delta = datetime.today() - timedelta(days=30)
print(delta.strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]) # 2022-06-09 08:31:52.299
CodePudding user response:
The last digits are added when you do the calculation with timedelta.
from datetime import datetime,timedelta
datetime_limit = str(datetime.today() - timedelta(days=30))[:-3]
print(datetime_limit)