0

I'm trying to convert a string date to epoch, but it doesn't seem to pick up the am or pm. The time always defaults to am.

I've tried this:

dt = '2015-05-04 5:55PM'
pattern = '%Y-%m-%d %H:%M%p'
epochDate = int(time.mktime(time.strptime(dt, pattern)))
print epochDate

# Result
1430684700

# Checking output
datetime.datetime.fromtimestamp(1430684700).strftime('%Y-%m-%d %H:%M:%S%p')

# Doesn't show PM
'2015-05-04 05:55:00AM'

I'm not sure what I've done wrong here?

CocaCola
  • 751
  • 3
  • 10
  • 21

1 Answers1

-1

You should use the python-dateutil library:

>>> import dateutil.parser
>>> dateutil.parser.parse('2015-05-04 5:55PM')
datetime.datetime(2015, 5, 4, 17, 55)
>>> dateutil.parser.parse('2015-05-04 5:55AM')
datetime.datetime(2015, 5, 4, 5, 55)
Simeon Visser
  • 118,920
  • 18
  • 185
  • 180