怎么用Python输出当前日期?

1 个回答

Coco老师 - 科普专家
擅长:编程教育

获取星期几

from datetime import datetime, date
  
dayOfWeek = datetime.now().weekday()
print(dayOfWeek)
  
dayOfWeek = datetime.today().weekday()
print(dayOfWeek)

注意:datetime类的weekday()方法可以获得datetime是星期几,注意weekday()返回的是0~6,是星期一到星期日。

python获取今日日期

import datetime
today = datetime.date.today()
print(today)

输出:2022-03-25

python获取昨日日期

import datetime
today = datetime.date.today()
zuotian =  today- datetime.timedelta(days=1)
print(zuotian)