rahul singh
rahul singh

Reputation: 369

how to get a 0:00:00.002000 time in string form like 2microsecond

Hello guys i have program where i'm getting a total time taken by the program to complete the operation for that code is:

from datetime import datetime
startTime = datetime.now()
.........................
.........................
print("Total Time Taken to uploading file- " + format(datetime.now() - startTime))

output:0:00:00.002000 or 0:00:20.912000 How do i convert decimal to like 2 microsecond or it print like 20 second_912 microsecond...Is there any module define in python for it? please help

Upvotes: 1

Views: 59

Answers (1)

Arount
Arount

Reputation: 10403

datetime.now() - startTime returns a timedelta object.

This object expose a total_seconds() method which output a float.

So you should be able to convert this delta to microseconds like:

from datetime import datetime
import time

startTime = datetime.now()
time.sleep(0.01)

seconds = (datetime.now() - startTime).total_seconds()
print(seconds)
micro = seconds * 1000
print(micro)

Ouput:

0.010115 # seconds
10.115   # micro seconds

Upvotes: 1

Related Questions