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

How can I change microseconds in a datetime?

How can I change my code to new output?

My code:

import datetime
x = datetime.datetime.today()
print(x) 
# Old Output: 2021-12-15 12:03:16.151803 

but I want to create new output (remove the 803 from 12:03:16.151803 and save whole new output 2021-12-15 12:03:16.151)

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

# New Output: 2021-12-15 12:03:16.151

>Solution :

The simplest way to do that is by slicing it

Example:

x = datetime.datetime.today().strftime("%Y-%m-%d %H:%M:%S.%f")[:-3]
print(x)
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