PythonDatetime类的isoweekday()方法
Isoweekday()是 DateTime 类的一个方法,它告诉给定日期的日期。它返回一个与特定日期相对应的整数。
Syntax: datetime.isoweekday()
Parameters: None
Return Value: It returns an integer which corresponds to a day as per the table
Integer Returned | Day of the week |
---|---|
1 | Monday |
2 | Tuesday |
3 | Wednesday |
4 | Thursday |
5 | Friday |
6 | Saturday |
7 | Sunday |
示例 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()]))
输出:
The 7/28 in the year 2010 has fallen on Wednesday
The 7/28 in the year 2011 has fallen on Thursday
The 7/28 in the year 2012 has fallen on Saturday
The 7/28 in the year 2013 has fallen on Sunday
The 7/28 in the year 2014 has fallen on Monday
The 7/28 in the year 2015 has fallen on Tuesday
The 7/28 in the year 2016 has fallen on Thursday
The 7/28 in the year 2017 has fallen on Friday
The 7/28 in the year 2018 has fallen on Saturday
The 7/28 in the year 2019 has fallen on Sunday
The 7/28 in the year 2020 has fallen on Tuesday
The 7/28 in the year 2021 has fallen on Wednesday