📜  PythonDatetime.date类的weekday()函数

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

PythonDatetime.date类的weekday()函数

datetime.date 类函数的weekday()用于返回与指定的星期几对应的整数值。

Integer returned by weekday() functionDay of the week
0Monday
1Tuesday
2Wednesday
3Thursday
4Friday
5Saturday
6Sunday

示例 1:获取一周中指定天数对应的整数值。



Python3
# Python3 code for getting
# integer value corresponding
# to the specified day of the week
 
# Importing datetime module
import datetime
 
# Specifying some date and time values
dateTimeInstance1 = datetime.datetime(2021, 8, 1, 00, 00, 00)
dateTimeInstance2 = datetime.datetime(2021, 8, 2, 00, 00, 00)
dateTimeInstance3 = datetime.datetime(2021, 8, 3, 00, 00, 00)
 
# Calling the weekday() functions over the
# above dateTimeInstances
dayOfTheWeek1 = dateTimeInstance1.weekday()
dayOfTheWeek2 = dateTimeInstance2.weekday()
dayOfTheWeek3 = dateTimeInstance3.weekday()
 
# Getting the integer value corresponding
# to the specified day of the week
print(dayOfTheWeek1)
print(dayOfTheWeek2)
print(dayOfTheWeek3)


Python3
# Python3 code for getting
# integer value corresponding
# to the specified day of the week.
 
# Importing datetime module
import datetime
 
# Mapping of the week day
weekDaysMapping = ("Monday", "Tuesday",
                   "Wednesday", "Thursday",
                   "Friday", "Saturday",
                   "Sunday")
 
# Specifying a date and time value
dateTimeInstance = datetime.datetime(2021, 8, 2,
                                     00, 00, 00)
 
# Calling the weekday() function over the above
# specified weekday and data time value
dayOfTheWeek = weekDaysMapping[dateTimeInstance.weekday()]
 
# Printing the date and time along with the
# corresponding week day name
print("{} is for {}".format(dateTimeInstance, dayOfTheWeek))
 
# Getting on next day
nextDay = dateTimeInstance.replace(day=3)
 
# Calling the weekday() function over the above
# specified weekday and data time value
dayOfTheWeek = weekDaysMapping[nextDay.weekday()]
 
# Printing the date and time along with the
# corresponding week day name
print("{} is for {}".format(nextDay, dayOfTheWeek))
 
# Getting on next day
nextDay = dateTimeInstance.replace(day=4)
 
# Calling the weekday() function over the above
# specified weekday and data time value
dayOfTheWeek = weekDaysMapping[nextDay.weekday()]
 
# Printing the date and time along with the
# corresponding week day name
print("{} is for {}".format(nextDay, dayOfTheWeek))
 
# Getting on next day
nextDay = dateTimeInstance.replace(day=5)
 
# Calling the weekday() function over the above
# specified weekday and data time value
dayOfTheWeek = weekDaysMapping[nextDay.weekday()]
 
# Printing the date and time along with the
# corresponding week day name
print("{} is for {}".format(nextDay, dayOfTheWeek))
 
# Getting on next day
nextDay = dateTimeInstance.replace(day=6)
 
# Calling the weekday() function over the above
# specified weekday and data time value
dayOfTheWeek = weekDaysMapping[nextDay.weekday()]
 
# Printing the date and time along with the
# corresponding week day name
print("{} is for {}".format(nextDay, dayOfTheWeek))
 
# Getting on next day
nextDay = dateTimeInstance.replace(day=7)
 
# Calling the weekday() function over the above
# specified weekday and data time value
dayOfTheWeek = weekDaysMapping[nextDay.weekday()]
 
# Printing the date and time along with the
# corresponding week day name
print("{} is for {}".format(nextDay, dayOfTheWeek))
 
# Getting on next day
nextDay = dateTimeInstance.replace(day=8)
 
# Calling the weekday() function over the above
# specified weekday and data time value
dayOfTheWeek = weekDaysMapping[nextDay.weekday()]
 
# Printing the date and time along with the
# corresponding week day name
print("{} is for {}".format(nextDay, dayOfTheWeek))


输出:

6
0
1

示例 2:获取日期和时间以及整周的日期名称。

蟒蛇3

# Python3 code for getting
# integer value corresponding
# to the specified day of the week.
 
# Importing datetime module
import datetime
 
# Mapping of the week day
weekDaysMapping = ("Monday", "Tuesday",
                   "Wednesday", "Thursday",
                   "Friday", "Saturday",
                   "Sunday")
 
# Specifying a date and time value
dateTimeInstance = datetime.datetime(2021, 8, 2,
                                     00, 00, 00)
 
# Calling the weekday() function over the above
# specified weekday and data time value
dayOfTheWeek = weekDaysMapping[dateTimeInstance.weekday()]
 
# Printing the date and time along with the
# corresponding week day name
print("{} is for {}".format(dateTimeInstance, dayOfTheWeek))
 
# Getting on next day
nextDay = dateTimeInstance.replace(day=3)
 
# Calling the weekday() function over the above
# specified weekday and data time value
dayOfTheWeek = weekDaysMapping[nextDay.weekday()]
 
# Printing the date and time along with the
# corresponding week day name
print("{} is for {}".format(nextDay, dayOfTheWeek))
 
# Getting on next day
nextDay = dateTimeInstance.replace(day=4)
 
# Calling the weekday() function over the above
# specified weekday and data time value
dayOfTheWeek = weekDaysMapping[nextDay.weekday()]
 
# Printing the date and time along with the
# corresponding week day name
print("{} is for {}".format(nextDay, dayOfTheWeek))
 
# Getting on next day
nextDay = dateTimeInstance.replace(day=5)
 
# Calling the weekday() function over the above
# specified weekday and data time value
dayOfTheWeek = weekDaysMapping[nextDay.weekday()]
 
# Printing the date and time along with the
# corresponding week day name
print("{} is for {}".format(nextDay, dayOfTheWeek))
 
# Getting on next day
nextDay = dateTimeInstance.replace(day=6)
 
# Calling the weekday() function over the above
# specified weekday and data time value
dayOfTheWeek = weekDaysMapping[nextDay.weekday()]
 
# Printing the date and time along with the
# corresponding week day name
print("{} is for {}".format(nextDay, dayOfTheWeek))
 
# Getting on next day
nextDay = dateTimeInstance.replace(day=7)
 
# Calling the weekday() function over the above
# specified weekday and data time value
dayOfTheWeek = weekDaysMapping[nextDay.weekday()]
 
# Printing the date and time along with the
# corresponding week day name
print("{} is for {}".format(nextDay, dayOfTheWeek))
 
# Getting on next day
nextDay = dateTimeInstance.replace(day=8)
 
# Calling the weekday() function over the above
# specified weekday and data time value
dayOfTheWeek = weekDaysMapping[nextDay.weekday()]
 
# Printing the date and time along with the
# corresponding week day name
print("{} is for {}".format(nextDay, dayOfTheWeek))

输出:

2021-08-02 00:00:00 is for Monday
2021-08-03 00:00:00 is for Tuesday
2021-08-04 00:00:00 is for Wednesday
2021-08-05 00:00:00 is for Thursday
2021-08-06 00:00:00 is for Friday
2021-08-07 00:00:00 is for Saturday
2021-08-08 00:00:00 is for Sunday