0

I'm trying to take a date, for example Aug 22, 2017 02:00 PM EDT and get the month, day, year from it.

month = re.findall(r'', date)[0]
day = re.findall(r'', date)[0]
year = re.findall(r'', date)[0]

I've started with something like this:

(.*)(?<=[a-zA-Z]{3}\s)

for the month. Is there a better way to do this?

Kenny
  • 2,124
  • 3
  • 33
  • 63

1 Answers1

1

You need to first convert to datetime and then extract the needed values like this (reusing the example):

from datetime import datetime

datetime_object = datetime.strptime('Jun 1 2005  1:33PM', '%b %d %Y %I:%M%p')

print(datetime_object.year, datetime_object.month, datetime_object.day)

From what I can see you probably won't need to specify the format but pass the string directly to the datetime.strptime function.

sophros
  • 14,672
  • 11
  • 46
  • 75
  • for the `EDT` part I'm trying to add on `%Z` but I keep getting an error. Is that maybe not supported? – Kenny Jan 23 '18 at 22:33
  • What is the format string that you are using? An example of a failure would be helpful too. – sophros Jan 24 '18 at 09:30
  • `Aug 22, 2017 02:00 PM EDT` the end part `EDT` (or could be PST or MST). I used a `%V` but I guess it doesn't know how to interpret it. – Kenny Jan 24 '18 at 18:27
  • I believe this may be one of the issues described in [this answer](https://stackoverflow.com/questions/3305413/python-strptime-and-timezones) – sophros Jan 24 '18 at 20:59