📌  相关文章
📜  PythonDatetime.date类的isoweekday()函数

📅  最后修改于: 2022-05-13 01:55:38.810000             🧑  作者: Mango

PythonDatetime.date类的isoweekday()函数

isoweekday()是一个函数,它返回一个整数,告诉给定的日期所属。根据下表,它返回的整数代表一天。

Integer ReturnedDay of the week
1Monday
2Tuesday
3Wednesday
4Thursday
5Friday
6Saturday
7Sunday

示例 1:该程序使用 DateTime 模块获取日期并告诉日期是哪一天。

Python3
# importing the datetime module
import datetime
  
# Creating an list which will
# be  used to retrieve the day of the
# week using the return value of the
# isoweekday() function
DaysList  = ["None",
              "Monday",
            "Tuesday",
            "Wednesday",
            "Thursday",
            "Friday",
            "Saturday",
            "Sunday"]
  
# Using the function today()
# to get today's date
CurrentDate = datetime.date.today()
print("Current Date is :", CurrentDate)
  
# Using the isoweekday() function to
# retrieve the day of the given date
day = CurrentDate.isoweekday()
print("The date", CurrentDate, "falls on",
      DaysList[day])


Python3
# importing the datetime module
import datetime
 
# Creating an dictionary with the return
# value as keys and the day as the value
# This is used to retrieve the day of the week
# using the return value of the isoweekday()
# function
DaysList = ["None",
            "Monday",
            "Tuesday",
            "Wednesday",
            "Thursday",
            "Friday",
            "Saturday",
            "Sunday"]
 
# Getting the  user's input
day = 18
month = 9
year = 2020
 
# Creating the datetime object for the user's
# input by using the date() function of datetime
# class
Date = datetime.date(year, month, day)
 
# Using the isoweekday() function
# to retrieve the day of the given date
day = Date.isoweekday()
 
print("The given date", Date, "falls on",
      DaysList[day])


Python3
# importing the datetime module
import datetime
 
# Creating an dictionary with the return
# value as keys and the day as the value
# This is used to retrieve the day of the week
# using the return value of the isoweekday()
# function
DaysList = ["None",
            "Monday",
            "Tuesday",
            "Wednesday",
            "Thursday",
            "Friday",
            "Saturday",
            "Sunday"]
 
# Getting the  user's input
day = 1
month = 1
year = 2021
day_fallen = "Friday"
 
# Creating the datetime object for the user's
# input by using the date() function of datetime
# class
Date = datetime.date(year, month, day)
 
# Using the isoweekday() function
# to retrieve the day of the given date
day = Date.isoweekday()
 
# Checking if the day is matching the user's
# day
if day_fallen.lower() == DaysList[day].lower():
    print("Yes, your given date", Date,
          "falls on your expected day i.e ",
          DaysList[day])
else:
    print("No, your given date", Date, "falls on",
          DaysList[day],
          "but not on", day_fallen)


输出:



Current Date is : 2021-08-18
The date 2021-08-18 falls on Wednesday

示例 2:在此示例中,我们将接受用户输入的日期并返回它所在的日期。

蟒蛇3

# importing the datetime module
import datetime
 
# Creating an dictionary with the return
# value as keys and the day as the value
# This is used to retrieve the day of the week
# using the return value of the isoweekday()
# function
DaysList = ["None",
            "Monday",
            "Tuesday",
            "Wednesday",
            "Thursday",
            "Friday",
            "Saturday",
            "Sunday"]
 
# Getting the  user's input
day = 18
month = 9
year = 2020
 
# Creating the datetime object for the user's
# input by using the date() function of datetime
# class
Date = datetime.date(year, month, day)
 
# Using the isoweekday() function
# to retrieve the day of the given date
day = Date.isoweekday()
 
print("The given date", Date, "falls on",
      DaysList[day])

输出:

The given date 2020-09-18 falls on Friday

示例 3:在此示例中,我们将获取用户输入的日期和日期,并返回是否为真

蟒蛇3

# importing the datetime module
import datetime
 
# Creating an dictionary with the return
# value as keys and the day as the value
# This is used to retrieve the day of the week
# using the return value of the isoweekday()
# function
DaysList = ["None",
            "Monday",
            "Tuesday",
            "Wednesday",
            "Thursday",
            "Friday",
            "Saturday",
            "Sunday"]
 
# Getting the  user's input
day = 1
month = 1
year = 2021
day_fallen = "Friday"
 
# Creating the datetime object for the user's
# input by using the date() function of datetime
# class
Date = datetime.date(year, month, day)
 
# Using the isoweekday() function
# to retrieve the day of the given date
day = Date.isoweekday()
 
# Checking if the day is matching the user's
# day
if day_fallen.lower() == DaysList[day].lower():
    print("Yes, your given date", Date,
          "falls on your expected day i.e ",
          DaysList[day])
else:
    print("No, your given date", Date, "falls on",
          DaysList[day],
          "but not on", day_fallen)

输出: