2

I have the list of times posted below and am having difficulties with converting it to the following:

Step 1: Convert to EST time in 24-hour format.

Step 2: Convert to 12 hour EST time format.

I have tried doing this in both Python and Pandas and I'm willing to use whatever is easiest. Any help would be greatly appreciated.

['00:05', '17:07', '23:05', '23:05', '23:10', '23:10', '00:10', '00:15', 
'00:40', '01:40', '02:10']
Nazim Kerimbekov
  • 4,712
  • 8
  • 34
  • 58
Cam
  • 105
  • 1
  • 10
  • 2
    Possible duplicate of [0-23 hour military clock to standard time (hh:mm)](https://stackoverflow.com/questions/31691007/0-23-hour-military-clock-to-standard-time-hhmm) – But I'm Not A Wrapper Class Jul 01 '18 at 22:02
  • Those are helpful, but I haven't seen any posts where there is a list of times involved. All I have seen are manually inputted time scenarios and I would ideally like to loop through the list (if doable). – Cam Jul 01 '18 at 22:11
  • what is the exact output format you'd like? should it include am/pm? – Matthew Story Jul 01 '18 at 22:13
  • ideally yes, please – Cam Jul 01 '18 at 22:22

1 Answers1

6

We can easily achieve what you're looking for with datetime:

>>> import datetime
>>> times = [ "08:00", "23:00" ]
>>> [datetime.datetime.strptime(time, "%H:%M").strftime("%I:%M %p") for time in times]
['08:00 AM', '11:00 PM']

This reads your time in with strptime and then outputs it with strftime inside a list comprehension. To convert this to EST with no daylight savings time (which is -05:00) you can use pytz:

>>> import pytz
>>> [datetime.datetime.strptime(time, "%H:%M").replace(tzinfo=pytz.utc).astimezone(pytz.timezone('EST')).strftime("%I:%M %p") for time in times]
['03:00 AM', '06:00 PM']

Which first marks the time as utc (replace(tzinfo=pytz.utc)) and then converts it to EST astimezone(pytz.timezone('EST')) before reformatting it to 12 hour time.

Going one step further to what I think you want, which is to get the time today in EDT (EST factoring in datetime) we can take a hint from [this question][2]:

converted = []
tz = pytz.timezone('America/New_York')
for time in times:
    time = datetime.datetime.strptime(time, "%H:%M").time()
    converted.append(datetime.datetime.now(pytz.utc).replace(hour=time.hour, minute=time.minute).astimezone(tz).strftime("%I:%M %p"))

Which will build what I believe you are looking for:

['04:00 AM', '07:00 PM']
Matthew Story
  • 3,573
  • 15
  • 26
  • Beautiful thank you, how would I go about getting this to EST time though? The list was originally in GMT time – Cam Jul 01 '18 at 22:20
  • So because EST may be in daylight savings time or not based on time of year, you need more info to be able to answer this. – Matthew Story Jul 01 '18 at 22:22
  • see this thread (including my answer) for more https://stackoverflow.com/questions/51084316/str-to-time-object-in-python-3/51084990#51084990 – Matthew Story Jul 01 '18 at 22:23
  • If we could somehow subtract 4 hours from each of these that is what I am looking for, sorry really new to this. I was able to accomplish this with timedelta in Pandas but not sure how this would look in just Python – Cam Jul 01 '18 at 22:25
  • i can provide that pretty easily. let me get you an update. – Matthew Story Jul 01 '18 at 22:27
  • Thank you so much! It looks like it is behind 1 hour but maybe that is my fault giving you bad information. Either way thank you for all of your help on this! I've been spending most of the day on here looking for solutions and have been struggling. – Cam Jul 01 '18 at 22:42
  • @CamGi i have provided conversion to EST, but EST is `-05:00` not `-04:00` (which is `EDT`). – Matthew Story Jul 01 '18 at 22:42
  • @CamGi updated once more to show you how to use today's offset for `EDT` – Matthew Story Jul 01 '18 at 23:00
  • 1
    Incredible, thank you Matthew! You have salvaged this day of frustration – Cam Jul 01 '18 at 23:19