4-4总提示类型错误
来源:4-4 编程练习
晓芳1
2020-10-05 19:32:26
# coding:utf-8
from datetime import datetime
# 定义一个str_字符串为2019-09-10 8:10:56
str_ = '2019-09-10 8:10:56'
# 将str_转换为日期函数2019-09-10 8:10:56
str_date = datetime.strftime(str_,'%Y-%m-%d %H:%M:%S')
print(type(str_date))
# 定义now_变量接收当前的日期时间
now_ = datetime.now()
# 将当前日期时间格式化为——四位的年份/月/日 时:分:秒
date_str = now_.strftime('%Y/%m/%d %H:%M:%S')
print(date_str)
老师帮我看一下 哪里有问题 总是提示我:TypeError: descriptor 'strftime' for 'datetime.date' objects doesn't apply to a 'str' object
2回答
同学你好,同学将str_转换为日期函数写错了,应该将datetime.strftime()
改为datetime.strptime()
如果我的回答解决了你的疑惑,请采纳,祝学习愉快~
划过天空
2020-10-06
你2个相反的转换用的是同一个函数 字符串转 datatime 是datetime.strptime(str_, '%Y-%m-%d %H:%M:%S')
相似问题