0

This has probably been asked like a million times, but I still didn't manage to do it.

The case is, that I need to calculate the time between two unix timestamps:

eg: 1494708797619 - 1494709197066

What I have so far:

from datetime import datetime
a = datetime.fromtimestamp(1494708797619/1000.0)
b = datetime.fromtimestamp(1494709197066/1000.0)
c = b-a
print(c)

But the answer is currently in datetime format like that:

0:06:39.447000

But I'd like it to be something like that:

399447000

Cheers!

E. Muuli
  • 3,940
  • 5
  • 22
  • 30

2 Answers2

2
from datetime import datetime
a = datetime.fromtimestamp(1494708797619/1000.0)
b = datetime.fromtimestamp(1494709197066/1000.0)
c = int((b-a).total_seconds() * 1000)
print(c)

Will output

399447

But shortest way will be 1494708797619 - 1494709197066 as Daniel noted

eli-bd
  • 1,533
  • 2
  • 17
  • 35
1

A timedelta object (which is what you get when you add/subtract two datetime or date objects) has a .total_seconds() method which returns the total seconds. Just multiply that by 1000.

c = b-a
c.total_seconds() * 1000
panatale1
  • 373
  • 1
  • 10