Python Snippets

Drew Sears edited this page Mar 1, 2017 · 2 revisions

Convert a string to a datetime:

>>> import datetime
>>> now_dt = datetime.datetime.now()
>>> now_dt
datetime.datetime(2017, 3, 1, 12, 11, 47, 52669)
>>> str(now_dt)
'2017-03-01 12:11:47.052669'
>>> now_str = str(now_dt).split('.').pop(0)
>>> now_str
'2017-03-01 12:11:47'
>>> now_dt2 = datetime.datetime.strptime(now_str, '%Y-%m-%d %H:%M:%S')
>>> now_dt2
datetime.datetime(2017, 3, 1, 12, 0, 47)
>>> 
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.