打印当前年月日的Python程序
在本文中,任务是编写一个Python程序来打印当前的年月日。
方法:
- 在Python中,为了打印由年月日组成的当前日期,它有一个名为 datetime 的模块。从 DateTime 模块,导入日期类
- 创建日期类的对象
- 调用 date 类的 today()函数来获取今天的日期。
- 通过使用创建的对象,我们可以打印今天的年、月、日(日期类的属性)。
Python3
# importing date class from datetime module
from datetime import date
# creating the date object of today's date
todays_date = date.today()
# printing todays date
print("Current date: ", todays_date)
# fetching the current year, month and day of today
print("Current year:", todays_date.year)
print("Current month:", todays_date.month)
print("Current day:", todays_date.day)
输出:
Current date: 2020-12-10
Current year: 2020
Current month: 12
Current day: 10