python 获取当天日期值

会有一股神秘感。 提交于 2020-02-08 12:37:55
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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!