Python中的datetime模块

Python中的datetime模块

本文是基于Windows系统环境,学习和测试datetime模块:

  • Windows 10

  • PyCharm 2018.3.5 for Windows (exe)

  • python 3.6.8 Windows x86 executable installer


1. 显示日期

  • 显示今天日期
import datetime
today = datetime.date.today()
print(today) #  2019-07-30
formatted_today=today.strftime('%y%m%d')
print(formatted_today) #  190730
  • 显示明天日期
import datetime
today = datetime.date.today()
print(today) #  2019-07-30
tomorrow = today + datetime.timedelta(days=1)
print(tomorrow) #  2019-07-31
  • 显示昨天日期
import datetime
today = datetime.date.today()
print(today) #  2019-07-30
yesterday= today + datetime.timedelta(days=-1)
print(yesterday) #  2019-07-29

你可能感兴趣的:(Python,python,datetime)