如何使用 Python 找到指定年份的第一天?


在本程式中,我們必須列印年份的第一天。我們必須根據使用者輸入獲取年份。

演算法

Step 1: Import the datetime library.
Step 2: Take year as input from the user.
Step 3: Get the first day of the year by passing month, day and year as parameters to the datetime.datetime() function
Step 4: Display the first day using strftime() function.

示例程式碼

線上演示

import datetime
year = int(input("Enter year: "))
firstday = datetime.datetime(year, 1,1)
print("First Day of ", year, " = ", firstday.strftime("%A"))

輸出

First Day of  2021  =  Friday

更新於:16-3-2021

5K+ 次瀏覽

開始您的職業生涯

完成課程獲取認證

開始學習
廣告
© . All rights reserved.