Need help in Time calculator

Tell us what’s happening:
Describe your issue in detail here.

Your code so far
Save New Duplicate & Edit Just Text Twitter

def add_time(start, duration, today=None):
    time = ""
    week = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]
    s_time, meridiem = start.split(" ")
    s_hour, s_minute = s_time.split(":")
    s_hour, s_minute = int(s_hour), int(s_minute)
    d_hour, d_minute = duration.split(":")
    d_hour, d_minute = int(d_hour), int(d_minute)
    minute = (s_minute + d_minute) % 60
    total_hours = (s_hour + d_hour) + ((s_minute + d_minute) // 60)
    days = total_hours // 24
    hour = total_hours % 24
    print('Minute:', minute)
    print("Total Hours:",total_hours)
    print("Hours:", hour)
    
    if hour >= 12:
        if hour > 12:
            hour %= 12 
        meridiem = "PM"
    else:
        meridiem = "AM"

    time += str(hour) + ":"

    if len(str(minute)) == 1:
        time += "0" + str(minute)
    else:
        time += str(minute)
    
    time += " " + meridiem

    print("Days: ",days) # Error in Days
    if days:
        next_day = days % 7
        if today:
            today = today.title()
            # print(((next_day + week.index(today)) % 7)) 
            time += ", " + week[((next_day + week.index(today) ) % 7)] # Code worked but logic is still need to be cleared.
        else:
            if next_day > 1:
                time += str(f"{next_day} days later")
            if next_day == 1:
                time += " (next day)"
    else:
        if today:
            time += ", "+ today.title()

    # if meridiem == "AM":
    #     if ((s_hour + d_hour) // 11) % 2 != 0:
    #         meridiem = "PM"
        
    # if meridiem == "PM":
    #     if ((s_hour + d_hour) // 11) % 2 == 0:
    #         meridiem = "AM"
    
        
    return time

print(add_time("11:59 PM", "24:05", "Wednesday")) # expected O/P "00:03 AM Thursday" 

""" add_time("3:00 PM", "3:10")
# Returns: 6:10 PM

add_time("11:30 AM", "2:32", "Monday")
# Returns: 2:02 PM, Monday

add_time("11:43 AM", "00:20")
# Returns: 12:03 PM

add_time("10:10 PM", "3:30")
# Returns: 1:40 AM (next day)

add_time("11:43 PM", "24:20", "tuesday")
# Returns: 12:03 AM, Thursday (2 days later)

add_time("6:30 PM", "205:12")
# Returns: 7:42 AM (9 days later) """

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36

Challenge: Time Calculator

Link to the challenge:

Hey can anyone help me out here?

Sure, can you tell us what the problem is?

Not getting proper logic which should be applied for days and meridiam.
For ex: Current time = “11:59 PM”,
Addition time = “24:05”,
Current day = “Wednesday”

Actual o/p - 12:04 PM, Thursday
Expected o/p - 12:04 AM, Friday.

Your meridian code assumes everything beyond 12 would be PM. However if the day changes, it could be AM again.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.