我是靠谱客的博主 纯情煎蛋,这篇文章主要介绍Python的time,datetime,string相互转换,现在分享给大家,希望可以做个参考。

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

  

转载于:https://www.cnblogs.com/huzixia/p/10391662.html

最后

以上就是纯情煎蛋最近收集整理的关于Python的time,datetime,string相互转换的全部内容,更多相关Python内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(115)

评论列表共有 0 条评论

立即
投稿
返回
顶部