Home > other >  Find the unit of time differencing 2 datetime range in python
Find the unit of time differencing 2 datetime range in python

Time:04-19

how to get the unit by differencing in 2 datetime.

i.e.,

datetime1 = 2020-06-29 16:15:27 datetime2 = 2020-06-29 16:17:27

unit = minute

datetime1 = 2020-06-29 16:15:27 datetime2 = 2020-06-29 17:17:27

unit = hour

datetime1 = 2020-06-29 16:15:27 datetime2 = 2020-06-31 17:17:27

unit = day

datetime1 = 2020-06-29 16:15:27 datetime2 = 2020-07-29 17:17:27

unit = month

CodePudding user response:

For the days, hours and minutes, you can have the foolowing approach:

Days

import datetime
  
a = datetime.datetime(2017, 6, 21, 18, 25, 30)
b = datetime.datetime(2017, 5, 16, 8, 21, 10)
  
ts_start=a
ts_end=b
ts_diff=ts_end-ts_start
secs=ts_diff.total_seconds()
days,secs=divmod(secs,secs_per_day:=60*60*24)

answer='Duration={} days'.format(int(days))

Hours

import datetime
  
a = datetime.datetime(2017, 6, 21, 18, 25, 30)
b = datetime.datetime(2017, 5, 16, 8, 21, 10)
  
ts_start=a
ts_end=b
ts_diff=ts_end-ts_start
secs=ts_diff.total_seconds()

hrs,secs=divmod(secs,secs_per_hr:=60*60)

answer='Duration={} hrs'.format(int(hrs))

Minutes

import datetime
  
a = datetime.datetime(2017, 6, 21, 18, 25, 30)
b = datetime.datetime(2017, 5, 16, 8, 21, 10)
  
ts_start=a
ts_end=b
ts_diff=ts_end-ts_start
secs=ts_diff.total_seconds()
mins,secs=divmod(secs,secs_per_min:=60)
answer='Duration={} mins'.format(int(mins))

seconds

import datetime
  
a = datetime.datetime(2017, 6, 21, 18, 25, 30)
b = datetime.datetime(2017, 5, 16, 8, 21, 10)
  
ts_start=a
ts_end=b
ts_diff=ts_end-ts_start
secs=ts_diff.total_seconds()



answer='Duration={} secs'.format(int(secs))

As for months, it is a little trickier. The best way to do this is given here Best way to find the months between two dates

CodePudding user response:

In python we can directly subtract datetime object using - operator

example

import datetime
  
# datetime(year, month, day, hour, minute, second)
a = datetime.datetime(2021, 6, 21, 18, 25, 30)
b = datetime.datetime(2020, 5, 16, 8, 21, 10)
  
# returns a timedelta object
c = a-b 
print('Difference: ', c)
  
minutes = c.total_seconds() / 60
hours = c.total_seconds()//3600
months = (a.year - b.year) * 12   (a.month - b.month)
days = c.days
  

CodePudding user response:

Create Enum to store the difference between two datetime objects

#!/usr/bin/env python3.10

from datetime import datetime
from enum import Enum, auto, unique

@unique
class DATETIME_UNIT(Enum):

    YEAR = auto()
    MONTH = auto()
    DAY = auto()
    HOUR = auto()
    MINUTE = auto()
    SECOND = auto()
    SAME = auto()

def getDateTimeUnit(datetime1:datetime, datetime2:datetime):
    unit = DATETIME_UNIT.SAME

    if datetime1.year != datetime2.year:
        unit = DATETIME_UNIT.YEAR
    elif datetime1.month != datetime2.month:
        unit = DATETIME_UNIT.YEAR
    elif datetime1.day != datetime2.day:
        unit = DATETIME_UNIT.DAY
    elif datetime1.hour != datetime2.hour:
        unit = DATETIME_UNIT.HOUR
    elif datetime1.minute != datetime2.minute:
        unit = DATETIME_UNIT.MINUTE
    elif datetime1.second != datetime2.second:
        unit = DATETIME_UNIT.SECOND
    else:
        unit = DATETIME_UNIT.SAME

    return unit

if __name__ == "__main__":
    #datetime1 = datetime.strftime("2020-06-29 16:15:27", "%yyy-%mm-           
  • Related