Convert timedelta to milliseconds python

Question:

I have the following time:

time = datetime.timedelta(days=1, hours=4, minutes=5, seconds=33, milliseconds=623)

Is it possible, to convert the time in milliseconds?
Like this:

101133623.0
Asked By: hubi3012

||

Answers:

It’s certainly possible.

1 day + 4 hours + 5 minutes + 33 seconds + 623 milliseconds =
24 * 60 * 60 seconds + 4 * 60 * 60 seconds + 5 * 60 seconds + 33 seconds + 0.623 seconds =
86400 seconds + 14400 seconds + 300 seconds + 33 seconds + 0.623 seconds =
101133.623 seconds

Just use multiplication

Function Below:

def timestamp_to_milliseconds(timestamp):

  day, hour, minute, second, millisecond = timestamp.split(":")
  seconds = int(day) * 24 * 60 * 60 + int(hour) * 60 * 60 + int(minute) * 60 + int(second)
  seconds += float(millisecond) / 1000
  milliseconds = seconds * 1000

  return milliseconds
Answered By: Istiak Hassan Emon

I found a possibility to solve the problem

import datetime

time = datetime.timedelta(days=1, hours=4, minutes=5, seconds=33, milliseconds=623)
result = time.total_seconds()*1000
print(result)

Answered By: hubi3012
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.