欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

python 获取当天日期值

程序员文章站 2022-03-09 18:19:02
...
import time, datetime
date = datetime.date.today()

报错:

AttributeError: ‘method_descriptor’ object has no attribute ‘today’

debug:You need do like this one (ipython output)

import time, datetime
from datetime import datetime
date = datetime.today().date()
print(date)

参考链接,回答三

import time, datetime
from datetime import datetime

date = datetime.today().date()
print(date)
print(type(date))
date2 = timedelta(days=1)
current_date = datetime.today().date().strftime("%Y-%m-%d")
yesterday = (datetime.today().date() - datetime.timedelta(days=1)).strftime("%Y-%m-%d")
current_date_3 = (datetime.today().date() - datetime.timedelta(days=3)).strftime("%Y-%m-%d")

报错:

AttributeError: type object ‘datetime.datetime’ has no attribute ‘timedelta’

import time, datetime
from datetime import datetime, timedelta

date = datetime.today().date()
print(date)
print(type(date))
date2 = timedelta(days=1)
current_date = datetime.today().date().strftime("%Y-%m-%d")
yesterday = (datetime.today().date() - timedelta(days=1)).strftime("%Y-%m-%d")
current_date_3 = (datetime.today().date() - timedelta(days=3)).strftime("%Y-%m-%d")

参考回答:I found this to be a lot easier