Jul-28-2021, 07:21 PM
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).