Skip to content

Instantly share code, notes, and snippets.

@buptxge
Last active March 14, 2016 01:22
Show Gist options
  • Save buptxge/1659fad862ef3f8ec957 to your computer and use it in GitHub Desktop.
Save buptxge/1659fad862ef3f8ec957 to your computer and use it in GitHub Desktop.
Python get structed time string
import datetime
import time
#Get now time string
timestr = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
#Calc time delta
t1 = datetime.datetime.now()
time.sleep(3)
t2 = datetime.datetime.now()
timedelta_str = str(t2-t1)
#Get datetime from a string and compare two datetime
t3 = datetime.datetime.strptime("2016-03-16 09:01:02","%Y-%m-%d %H:%M:%S")
t4 = datetime.datetime.strptime("2016-03-15 18:03:27","%Y-%m-%d %H:%M:%S")
result = t3<t4 #False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment