python代码获取今天昨天明天的日期

Posted Data+Science+Insight

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python代码获取今天昨天明天的日期相关的知识,希望对你有一定的参考价值。

python代码获取今天、昨天、明天的日期

 

See the source image

#python代码获取今天、昨天、明天的日期

# Python program to find yesterday,
# today and tomorrow


# Import datetime and timedelta
# class from datetime module
from datetime import datetime, timedelta


# Get today's date
presentday = datetime.now() # or presentday = datetime.today()

# Get Yesterday
yesterday = presentday - timedelta(1)

# Get Tomorrow
tomorrow = presentday + timedelta(1)


# strftime() is to format date according to
# the need by converting them to string
print("Yesterday = ", yesterday.strftime('%d-%m-%Y'))
print("Today = ", presentday.strftime('%d-%m-%Y'))
print("Tomorrow = ", tomorrow.strftime('%d-%m-%Y'))
Yesterday =  13-06-2021
Today =  14-06-2021
Tomorrow =  15-06-2021

 

参考:Python | Find yesterday’s, today’s and tomorrow’s date

参考:Find yesterday’s, today’s and tomorrow’s date in Python

 

以上是关于python代码获取今天昨天明天的日期的主要内容,如果未能解决你的问题,请参考以下文章