navya sri
navya sri

Reputation: 428

How to subtract the two date time objects using python

I want to subtract the two date time object using python those are in utc format("%Y-%m-%d %H:%M:%S.%f").How can i subtract the two object, the output should be in ITC format(("%Y-%m-%d %H:%M:%S.%f").Can anyone please help me.

Given below is my sample example:

login in-time: 2019-04-23 04:22:50.421406
logout time:2019-04-23 04:34:18.002699

Upvotes: 0

Views: 152

Answers (2)

Sankar guru
Sankar guru

Reputation: 1015

Subtract the two date time objects:

from datetime import datetime


t1 = datetime(2019,4,23,8,22,50,421406)
t2 = datetime(2019,4,23,4,55,18,155555)
t3 = t1 -t2
print(t3)

Upvotes: 1

Chris
Chris

Reputation: 29742

Use dateutil.parser:

import dateutil.parser as dparser

in_time = 'login in-time: 2019-04-23 04:22:50.421406'
out_time = 'logout time:2019-04-23 04:34:18.002699'

str((dparser.parse(out_time, fuzzy=True) - dparser.parse(in_time, fuzzy=True)))

Output:

'0:11:27.581293'

Upvotes: 2

Related Questions