Calculate tow date difference in python
from datetime import datetime, time
def date_diff_in_seconds(date2, date1):
timedelta = date2 - date1
return timedelta.days * 24 * 3600 + timedelta.seconds
def calculate_seconds(seconds):
minutes, seconds = divmod(seconds, 60)
hours, minutes = divmod(minutes, 60)
days, hours = divmod(hours, 24)
return (days, hours, minutes, seconds)
#Specified date
first_date = datetime.strptime('2019-07-19 01:00:00', '%Y-%m-%d %H:%M:%S')
#Current date
second_date = datetime.now()
print("\n%d day(s), %d hour(s), %d minute(s), %d second(s)" % calculate_seconds(date_diff_in_seconds(second_date, first_date)))
Comments
Post a Comment