python str datetime date 之间的转换
- str 转datetime
# str2datetime
In [8]: datetime.datetime.strptime('20200101','%Y%m%d')
Out[8]: datetime.datetime(2020, 1, 1, 0, 0)
# datetime2date
In [9]: datetime.datetime.now().date()
Out[9]: datetime.date(2021, 10, 9)
# datetime2str
In [10]: str(datetime.datetime.now())
Out[10]: '2021-10-09 15:40:07.681306'
# date2str
In [11]: str(datetime.date.today())
Out[11]: '2021-10-09'
# date init
In [13]: datetime.date(2020,1,1)
Out[13]: datetime.date(2020, 1, 1)
# str2date
In [14]: datetime.datetime.strptime('20200101','%Y%m%d').date()
Out[14]: datetime.date(2020, 1, 1)
本文来自博客园,作者:那时一个人,转载请注明原文链接:https://www.cnblogs.com/qianxunman/p/15386085.html