📜  PythonDatetime类的isoweekday()方法

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

PythonDatetime类的isoweekday()方法

Isoweekday()是 DateTime 类的一个方法,它告诉给定日期的日期。它返回一个与特定日期相对应的整数。

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

示例 1:打印当前日期的日期。



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
weekdays = {1: "Monday",
            2: "Tuesday",
            3: "Wednesday",
            4: "Thursday",
            5: "Friday",
            6: "Saturday",
            7: "Sunday"}
 
# Getting current date using today()
# function of the datetime class
todays_date = datetime.date.today()
print("Today's date is :", todays_date)
 
# Using the isoweekday() function to
# retrieve the day of the given date
day = todays_date.isoweekday()
print("The date", todays_date, "falls on",
      weekdays[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
weekdays = {1: "Monday",
            2: "Tuesday",
            3: "Wednesday",
            4: "Thursday",
            5: "Friday",
            6: "Saturday",
            7: "Sunday"}
 
# Getting current year using today() function
# of the datetime class and the year attribute
Today = datetime.date.today()
current_year = Today.year
 
for i in range(2010, current_year+1):
    # Printing the day of the year
    # by first creating an datetime object
    # for the starting day of the year and
    # then we use isoweekday
    # to get the value and we use the
    # weekdays to retrieve the day of the year
    print("The {}/{} in the year {} has fallen on {}".\
          format(Today.month, Today.day, i,
          weekdays[datetime.date(i, Today.month,
                    Today.day).isoweekday()]))


输出:

Today's date is : 2021-07-27
The date 2021-07-27 falls on Tuesday

示例 2:获取从 2010 年到当年的今天日期的星期几

蟒蛇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
weekdays = {1: "Monday",
            2: "Tuesday",
            3: "Wednesday",
            4: "Thursday",
            5: "Friday",
            6: "Saturday",
            7: "Sunday"}
 
# Getting current year using today() function
# of the datetime class and the year attribute
Today = datetime.date.today()
current_year = Today.year
 
for i in range(2010, current_year+1):
    # Printing the day of the year
    # by first creating an datetime object
    # for the starting day of the year and
    # then we use isoweekday
    # to get the value and we use the
    # weekdays to retrieve the day of the year
    print("The {}/{} in the year {} has fallen on {}".\
          format(Today.month, Today.day, i,
          weekdays[datetime.date(i, Today.month,
                    Today.day).isoweekday()]))

输出: