Home > Backend Development > Python Tutorial > python time, datetime, string conversion

python time, datetime, string conversion

巴扎黑
Release: 2016-12-09 11:18:31
Original
1586 people have browsed it

# 把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", tiem.localtime(stamp))
      
# 把datetime类型转外时间戳形式
def datetime_toTimestamp(dateTim):
    return time.mktime(dateTim.timetuple())
Copy after login


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template