Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

python datetime calculation not trimming to milliseconds

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)

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading