1

How to convert this string to Unix time? t = '20160224122738'

I can write a function to do this, but wanted to know if there was an already-existing simple method that I could use.

Serg
  • 11
  • 2

2 Answers2

2

An easy way that comes to mind would be:

>>> import time
>>> import datetime
>>> t = '20160224122738'
>>> time.mktime(datetime.datetime.strptime(t, "%Y%m%d%H%M%S").timetuple())
1456309658.0
Alexander Ejbekov
  • 5,594
  • 1
  • 26
  • 26
1

Try this,

>>> import datetime
>>> t = '20160224122738'
>>> int(datetime.datetime.strptime(t, '%Y%m%d%H%M%S').strftime("%s"))
1456297058
Savad KP
  • 1,625
  • 3
  • 28
  • 40