天天看點

Python程式可列印今天的年,月和日

In the below example – we are implementing a

python program to print the current/ today's year, month and year

.

在下面的示例中-我們正在實作一個

python程式來列印目前/今天的年,月和年

Steps: 腳步:
  • Import the date class from datetime module

    從datetime子產品導入日期類

  • Create a date object of today's date – call the today() function of date class to get the current date.

    建立今天日期的日期對象–調用date類的today()函數以擷取目前日期。

  • By using the date object – we can extract and print today's year, month and day.

    通過使用日期對象,我們可以提取和列印今天的年,月和日。

# Python program to
# print today's year, month and day

# importing the date class datetime module
from datetime import date

# creating the date object of today's date
current_date = date.today() 

# printing the current date
print("Current date: ", current_date)

# extracting the current year, month and day
print("Current year:", current_date.year)
print("Current month:", current_date.month)
print("Current day:", current_date.day)
           
Output 輸出量
Current date:  2020-03-09
Current year: 2020
Current month: 3
Current day: 9
           
翻譯自: https://www.includehelp.com/python/program-to-print-todays-year-month-and-day.aspx