Python日期格式和字符串格式相互轉換的方法
更新時間:2020年02月18日 12:50:09 作者:陶士涵
這篇文章主要介紹了Python日期格式和字符串格式相互轉換的方法,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
由字符串格式轉化為日期格式的函數為: datetime.datetime.strptime()
由日期格式轉化為字符串格式的函數為: datetime.datetime.strftime()
# encoding: utf-8
import datetime
day = datetime.datetime.strptime('2020-2-18 10:54:45', '%Y-%m-%d %H:%M:%S')
print(day)
print type(day)
day = datetime.datetime.strftime(day, '%Y-%m-%d %H:%M:%S')
print(day)
print type(day)
root@tao:/var/www/html/python# python 3.py
2020-02-18 10:54:45
<type 'datetime.datetime'>
2020-02-18 10:54:45
<type 'str'>
總結
以上所述是小編給大家介紹的Python日期格式和字符串格式相互轉換的方法,希望對大家有所幫助
相關文章
python 內置函數-range()+zip()+sorted()+map()+reduce()+filte
這篇文章主要介紹了python 內置函數-range()+zip()+sorted()+map()+reduce()+filter(),想具體了解函數具體用法的小伙伴可以參考一下下面的介紹,希望對你有所幫助2021-12-12

