老师问下这个
来源:2-8 编程练习
SomnusL
2019-11-21 00:42:11
from datetime import datetime
now_time=datetime.now()
print("NowTime:{}".format(now_time.date()))
print("NowTime:{}".format(datetime.today()))
print(("NowDate:{}".format(now_time.date())))
year=datetime.now().year
month=datetime.now().month
day=datetime.now().day
print("当前日日期为{},{},{}".format(year,month,day))
我感觉我没搞清楚now_time=datetime.now()用now_time调用和导入datetime直接调用的区别 弄混了调用代码也不会报错,输出就报错
1回答
好帮手乔木
2019-11-21
同学你好:
变量now_time 是<class 'datetime.datetime'>的对象,同学没有使用now_time对象,重新使用datetime模块创建一个新的<class 'datetime.datetime'>的对象。
now_time 对象和新的对象之间没有关系。
date()方法是datetime.now()对象可以使用的方式,生成一个<class 'datetime.date'>对象。
同学使用datetime().date()则需要向date()中传递参数(时间对象)。
print("NowTime:{}".format(datetime.date(now_time)))

同学可以多练习使用这个模块常用方法的用法。
如果我解决了同学的问题,请采纳!学习愉快^_^。
相似问题