Problem with calculating how many times some date has passed midnight

Question:

I would like to ask for a help. I am a beginner when it comes to Python. I try to write a function, that sums up together two "times" and returns new_time and also how many times new_time passed midnight of "start_time"(for example 23:00 and 03:00, new_date is 02:00, and 1 day has passed )

Thank you really much in advance

from datetime import datetime, timedelta


def add_time(start_time: str, time_to_add: str):
    start_time_time = datetime.strptime(start_time, "%H:%M")
    add_time_time = datetime.strptime(time_to_add, "%H:%M")
    new_time = start_time_time + timedelta(minutes=add_time_time.minute, hours=add_time_time.hour)

    return f"New time is {new_time.strftime('%H:%M')}, XXX days after"

print(add_time("23:20", "19:20"))
Asked By: Oliver Jakubec

||

Answers:

Calculate the dates for start_time_time and new_time. The number of days elapsed will be the difference (in days) between these dates.

I believe there are several ways to extract just the date from a "datetime", but I have just replaced the hours and minutes to zero.

from datetime import datetime, timedelta

def add_time(start_time: str, time_to_add: str):
    start_time_time = datetime.strptime(start_time, "%H:%M")
    start_date = start_time_time.replace(hour=0, minute=0)
    #print(start_date)
    add_time_time = datetime.strptime(time_to_add, "%H:%M")
    new_time = start_time_time + timedelta(minutes=add_time_time.minute, hours=add_time_time.hour)
    new_date = new_time.replace(hour=0, minute=0)
    #print(new_date)
    
    days_elapsed = (new_date - start_date).days
    
    return f"New time is {new_time.strftime('%H:%M')}, {days_elapsed} days after"

print(add_time("23:20", "19:20"))

The following code snippets demonstrates how to calculate the number of days after. You can uncomment the print statements to see what these dates actually represent.

Hope this helps.

Answered By: DrCorgi

There are many ways to do this, I done mine in such as way that it should allow you to add example of 100hours, etc. Hope this helps.

from datetime import datetime, timedelta

# Function that adds time HH:mm to a datetime object, adds set Hours and Minutes to start time and returns total days, hours, minutes and seconds passed
def add_time(start_time, time_to_add):
    # Add time to start time
    start_time = datetime.strptime(start_time, '%H:%M')
    # Strip hours, minutes and convert to ms
    time_to_add = time_to_add.split(':')
    time_to_add = timedelta(hours=int(time_to_add[0]), minutes=int(time_to_add[1]))
    finish_time = start_time + time_to_add

    # Calculate total days, hours, minutes and seconds passed
    total_days = finish_time.day - start_time.day
    total_hours = finish_time.hour - start_time.hour
    total_minutes = finish_time.minute - start_time.minute
    total_seconds = finish_time.second - start_time.second

    # Return total days, hours, minutes and seconds passed
    return total_days, total_hours, total_minutes, total_seconds

# today + 23 hours + 20 minutes
days, hours, minutes, seconds = add_time("13:13", "25:00")

print(days, hours, minutes, seconds)
Answered By: Aidan Donnelly
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.