Maison  >  Article  >  développement back-end  >  python time,datetime,string转换

python time,datetime,string转换

巴扎黑
巴扎黑original
2016-12-09 11:18:311521parcourir

# 把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())


Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Article précédent:python 时间格式化Article suivant:Python的中文字符乱码