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")