当前第2页 返回上一页
(4)dateTime类型转为str类型
1 2 3 4 | import datetime
dateTime_p = datetime.datetime.now()
str_p = datetime.datetime. strftime (dateTime_p, '%Y-%m-%d' )
print (dateTime_p) # 2019-01-30 15:36:19.415157
|
(5)字符串类型str转换为date类型
1 2 3 4 5 | #!/usr/bin/env python3
import datetime
str_p = '2019-01-30'
date_p = datetime.datetime. strptime (str_p, '%Y-%m-%d' ). date ()
print (date_p,type(date_p)) # 2019-01-30 < class 'datetime.date' >
|
另外dateTime类型和date类型可以直接做加1减1这种操作
1 2 3 4 5 6 7 8 9 | #!/usr/bin/env python3
import datetime
# today = datetime.datetime.today()
today = datetime.datetime.today(). date ()
yestoday = today + datetime.timedelta(days=-1)
tomorrow = today + datetime.timedelta(days=1)
print (today) # 2019-01-30
print (yestoday)# 2019-01-29
print (tomorrow)# 2019-01-31
|
以上就是如何把字符串转化成时间的详细内容,更多文章请关注木庄网络博客!!
返回前面的内容
相关阅读 >>
Python中的while什么意思
Python使用四种方法实现获取当前页面内所有链接的对比分析
Python如何查看变量类型
Python和spyder的区别
Python模块和包的区别
Python可以开发网页吗
Python 怎么求平均值
Python中如何从列表中删除none值
Python是什么?
Python中有关filter的用法详解
更多相关阅读请进入《Python》频道 >>
人民邮电出版社
python入门书籍,非常畅销,超高好评,python官方公认好书。
转载请注明出处:木庄网络博客 » 如何把字符串转化成时间