Converting 'Time' to AM/PM - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Converting 'Time' to AM/PM (/thread-34394.html) Pages:
1
2
|
Converting 'Time' to AM/PM - tester_V - Jul-27-2021 Greetings to all! I'm sure I'm trying to invent a bicycle. I need to convert strings in the following format to AM/PM: st = '2021-07-05-231403'I wrote a small snipped and it does what I want but ti looks crude, there is no elegance in it. st = '2021-07-05-221403' ya,mo,da,hms =st.split("-") print(f" HMS -> {hms}") hh=hms[:2] mm=hms[2:4] ss=hms[4:6] hh=int(hh) print(f" Hours ---> {hh}") print(f" Minutes -> {mm}") print(f" Seconds -> {ss}") if hh == 23 : hh=11 print(f" {hh}:PM") # <- Converted to PM hh=str(hh) out=mm+"/"+da+"/"+ya+" "+hh+":"+mm+":"+ss+" "+"PM" print(f" Converted to AM/PM -> {out}") if hh == 22 : hh=10 print(f" {hh}:PM") # <- Converted to PM hh=str(hh) out=mm+"/"+da+"/"+ya+" "+hh+":"+mm+":"+ss+" "+"PM" print(f" Converted to AM/PM -> {out}")Thank you! RE: Converting 'Time' to AM/PM - bowlofred - Jul-27-2021 For anything real, please use a time library to print it in the format you want. from datetime import datetime st = '2021-07-05-231403' d = datetime.strptime(st, "%Y-%m-%d-%H%M%S") print(d) print(datetime.strftime(d, "%Y-%m-%d %I:%M:%S %p")) If you're just looking at it as an exercise, then you're already doing all the work to parse out the hour. All you need to do then is map different hours to AM/PMall_hours = range(24) def hour24_to_ampm(hour): ampm = "AM" if hour < 12 else "PM" new_hour = (hour - 1) % 12 + 1 return new_hour, ampm for hour in all_hours: new_hour, ampm = hour24_to_ampm(hour) print(f"{hour} -> {new_hour} {ampm}")
RE: Converting 'Time' to AM/PM - tester_V - Jul-28-2021 bowlofred! You are DA MAN! It is not an exercise, I'm trying to process log files and they all have different times, 24hours, AM/PM... I love the first one, formating the sting it looks great! Thank you! RE: Converting 'Time' to AM/PM - tester_V - Jul-28-2021 I found a problem. The time strings are always converted to PM times, regardless if it is less or more than 12. I tried to compare it against "12" but the code fails. from datetime import datetime tms= ['2021-07-05-231403','2021-07-05-221402','2021-07-05-214001','2020-07-05-221400','2021-07-05-121404','2021-07-05-131405','2021-07-05-141406'] for et in tms : et=et.strip() hr = int(et[11:13]) #print(hr) if hr <12 : print(f" AM - {hr}") d = datetime.strptime(et, "%Y-%m-%d-%H%M%S") print(d) d2 =(datetime.strftime(d, "%d/%m/%Y %I:%M:%S %p")) print(f" AM --> {d2}") if hr >12 : print(f" PM - {hr}") d = datetime.strptime(et, "%Y-%m-%d-%H%M%S") print(d) d2 =(datetime.strftime(d, "%d/%m/%Y %I:%M:%S %p")) print(f" PM --> {d2}") RE: Converting 'Time' to AM/PM - tester_V - Jul-28-2021 My bad! The time strings list has a bad format . Sorry about that! I made changes it seems it is forking now. from datetime import datetime tms= ['2021-07-05-231403','2021-07-05-221402','2021-07-05-214001','2020-07-05-221400','2021-07-05-121404','2021-07-05-111405','2021-07-05-101406'] for et in tms : et=et.strip() hr = int(et[11:13]) if (hr <=12) : #print(f" AM - {hr}") d = datetime.strptime(et, "%Y-%m-%d-%H%M%S") d2 =(datetime.strftime(d, "%d/%m/%Y %I:%M:%S %p")) print(f" AM --> {d2}") if (hr >12) : #print(f" PM - {hr}") d = datetime.strptime(et, "%Y-%m-%d-%H%M%S") d2 =(datetime.strftime(d, "%d/%m/%Y %I:%M:%S %p")) print(f" PM --> {d2}") RE: Converting 'Time' to AM/PM - Yoriz - Jul-28-2021 None of the times in the list are below 12. Also, 12 will not be included only above or below 12. RE: Converting 'Time' to AM/PM - tester_V - Jul-28-2021 what do you mean? I'm confused now. The last three elements of the list are = or less than 12: '2021-07-05-121404','2021-07-05-111405','2021-07-05-101406' RE: Converting 'Time' to AM/PM - Yoriz - Jul-28-2021 They weren't on the post I was replying to, I got delayed in making the post, in the meantime, you had posted again RE: Converting 'Time' to AM/PM - bowlofred - Jul-28-2021 if (hr <=12) :This doesn't make sense to me. 11:22 is AM, but 12:22 is PM. I would have thought you'd check hr < 12 You're repeating your code. And you're checking things the library could tell you. Since both stanzas do the identical conversion, what is the utility of the "if"? I'd probably rewrite it like this: from datetime import datetime tms= ['2021-07-05-231403','2021-07-05-221402','2021-07-05-214001','2020-07-05-221400','2021-07-05-121404','2021-07-05-111405','2021-07-05-101406'] for et in tms : et=et.strip() d = datetime.strptime(et, "%Y-%m-%d-%H%M%S") d2 =datetime.strftime(d, "%d/%m/%Y %I:%M:%S %p") ampm = datetime.strftime(d, "%p") print(f" {ampm} --> {d2}")No duplicated code, and the AM or PM bit matches what I'd expect (times after noon are PM, not AM). RE: Converting 'Time' to AM/PM - snippsat - Jul-28-2021 Pendulum is cool bye fair the best date library for Python,i use it always if have deal with timezones. import pendulum tms = [ "2021-07-05-231403", "2021-07-05-221402", "2021-07-05-214001", "2020-07-05-221400", "2021-07-05-121404", "2021-07-05-111405", "2021-07-05-101406", ] for et in tms: dt = pendulum.parse(et, strict=False) print(dt.format('DD/MM/YYYY hh:mm:ss A')) #print(dt.to_day_datetime_string()) #print(dt.to_cookie_string())
|