Skip to content

Instantly share code, notes, and snippets.

@honmaple
Created September 23, 2016 01:14
Show Gist options
  • Save honmaple/1c8e0c72ce4f99e411206ac37880c04f to your computer and use it in GitHub Desktop.
Save honmaple/1c8e0c72ce4f99e411206ac37880c04f to your computer and use it in GitHub Desktop.
python时间或时间戳与字符串转换
#把datetime转成字符串
def datetime_toString(dt):
return dt.strftime("%Y-%m-%d-%H")
#把字符串转成datetime
def string_toDatetime(string):
return datetime.strptime(string, "%Y-%m-%d-%H")
#把字符串转成时间戳形式
def string_toTimestamp(strTime):
return time.mktime(string_toDatetime(strTime).timetuple())
#把时间戳转成字符串形式
def timestamp_toString(stamp):
return time.strftime("%Y-%m-%d-%H", time.localtime(stamp))
#把时间戳转成datetime
def timestamp_toDatetime(stamp):
return datetime.fromtimestamp(stamp)
#把datetime类型转外时间戳形式
def datetime_toTimestamp(dateTim):
return time.mktime(dateTim.timetuple())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment