Python AttributeError: 类型对象 'datetime.date' 没有属性 'now'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15434793/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 20:06:15  来源:igfitidea点击:

AttributeError: type object 'datetime.date' has no attribute 'now'

pythondjango

提问by Prometheus

Using these lines of code:

使用这些代码行:

from datetime import date
self.date_start_processing = date.now()

I'm getting this error:

我收到此错误:

AttributeError: type object 'datetime.date' has no attribute 'now'

AttributeError: 类型对象 'datetime.date' 没有属性 'now'

How can I solve this?

我该如何解决这个问题?

Thanks.

谢谢。

采纳答案by Aldarund

You need to use

你需要使用

 import datetime

 now = datetime.datetime.now()

Or if you are using django 1.4+ and have timezone enabled you should use

或者,如果您使用的是 django 1.4+ 并启用了时区,则应使用

 django.utils.timezone.now()