Python
 Computer >> コンピューター >  >> プログラミング >> Python

Pythonを使用して特定の年の最初のデートを見つける方法は?


このプログラムでは、年の最初の日を印刷する必要があります。ユーザー入力として1年かかる必要があります。

アルゴリズム

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

  1. Pythonを使用して週の最初の日を取得するにはどうすればよいですか?

    calenderモジュールを使用すると、Pythonを使用して週の最初の曜日を取得できます。 calenderモジュールには、毎週開始する平日の現在の設定を返す特別な関数firstweekday()があります。 例 import calendar print(calendar.firstweekday()) calendar.setfirstweekday(2) print(calendar.firstweekday()) 出力 これにより出力が得られます- 6 2

  2. Pythonを使用して今週を取得するにはどうすればよいですか?

    YOUは、datetimeモジュールのisocalender関数を使用して、現在の週を取得できます。最初に日付のオブジェクトを作成してから、このオブジェクトに対してisocalender()を呼び出します。これにより、3タプルの年、週番号、および曜日が返されます。 例 import datetime my_date = datetime.date.today() # if date is 01/01/2018 year, week_num, day_of_week = my_date.isocalendar() print("Week #" + str(week_num)