0

I'd like to convert this time Sat, 19 May 2018 16:32:56 +0000 to 20180519-113256 in a local timezone (EDT in this example) in python. Could anybody show me how to do it?

PS., The following example shows how to convert time to local timezone. But I am not sure how to parse Sat, 19 May 2018 16:32:56 +0000.

Convert UTC datetime string to local datetime with Python

user1424739
  • 11,937
  • 17
  • 63
  • 152

2 Answers2

1

You could choose any timezone you want:

import pytz
from datetime import datetime

s = 'Sat, 19 May 2018 16:32:56 +0000'
dt = datetime.strptime(s, '%a, %d %b %Y %H:%M:%S %z')
tz = pytz.timezone('America/Chicago')
new_s = dt.astimezone(tz).strftime('%Y%m%d-%H%M%S')
d2718nis
  • 1,279
  • 9
  • 13
0

for me this works:

from datetime import datetime
from dateutil import tz

def convert(date, from_zone = 'UTC', to_zone='America/New_York'):
    from_zone = tz.gettz(from_zone)
    to_zone = tz.gettz(to_zone)
    date = date.replace(tzinfo=from_zone)
    central = date.astimezone(to_zone)
    return date

s = "Sat, 19 May 2018 16:32:56 +0000"
d = datetime.strptime(s, '%a, %d %B %Y %H:%M:%S +%f')
d = convert(d)
Oliver Wilken
  • 2,654
  • 1
  • 24
  • 34