Python Forum
Need to sum up HHMMS AM/PM DD/MM/YYYY and a HH:MM:SS.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need to sum up HHMMS AM/PM DD/MM/YYYY and a HH:MM:SS.
#2
Using datetime
import datetime


start_string = "08:31:27 PM 01/02/2023"
total_time_string = "01:22:14"
format_string = "%I:%M:%S %p %d/%m/%Y"
start_datetime = datetime.datetime.strptime(start_string, format_string)
hours, minutes, seconds = total_time_string.split(":")
total_timedelta = datetime.timedelta(
    hours=float(hours), minutes=float(minutes), seconds=float(seconds)
)
end_datetime = start_datetime + total_timedelta
print(
    f"{start_datetime.strftime(format_string)}, {end_datetime.strftime(format_string)}"
)
Output:
08:31:27 PM 01/02/2023, 09:53:41 PM 01/02/2023
Reply


Messages In This Thread
RE: Need to sum up HHMMS AM/PM DD/MM/YYYY and a HH:MM:SS. - by Yoriz - Jan-04-2023, 08:33 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  regex to extract only yy or yyyy metalray 2 3,612 Jul-11-2018, 07:57 PM
Last Post: volcano63

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020