Weekday() method of datetime class in Python

Method Name:

weekday

 

Method Signature:

weekday()

 

 

Method Overview:

  • Returns the integer mapping corresponding the day of the week.

 

Return Values:

 

Integer returned by weekday()

Day of the week

0

Monday

1

Tuesday

2

Wednesday

3

Thursday

4

Friday

5

Saturday

6

Sunday

 

 

Example:

 

import datetime

 

weekDaysMapping = ("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday")

 

dateTimeInstance = datetime.datetime(2017,1,2,00,00,00)

dayOfTheWeek = weekDaysMapping[dateTimeInstance.weekday()]

print("{} falls on a {}".format(dateTimeInstance, dayOfTheWeek))

 

nextDay = dateTimeInstance.replace(day=3)

dayOfTheWeek = weekDaysMapping[nextDay.weekday()]

print("{} falls on a {}".format(nextDay, dayOfTheWeek))

 

nextDay = dateTimeInstance.replace(day=4)

dayOfTheWeek = weekDaysMapping[nextDay.weekday()]

print("{} falls on a {}".format(nextDay, dayOfTheWeek))

 

nextDay = dateTimeInstance.replace(day=5)

dayOfTheWeek = weekDaysMapping[nextDay.weekday()]

print("{} falls on a {}".format(nextDay, dayOfTheWeek))

 

 

nextDay = dateTimeInstance.replace(day=6)

dayOfTheWeek = weekDaysMapping[nextDay.weekday()]

print("{} falls on a {}".format(nextDay, dayOfTheWeek))

 

nextDay = dateTimeInstance.replace(day=7)

dayOfTheWeek = weekDaysMapping[nextDay.weekday()]

print("{} falls on a {}".format(nextDay, dayOfTheWeek))

 

nextDay = dateTimeInstance.replace(day=8)

dayOfTheWeek = weekDaysMapping[nextDay.weekday()]

print("{} falls on a {}".format(nextDay, dayOfTheWeek))

 

 

Output:

2017-01-02 00:00:00 falls on a Monday

2017-01-03 00:00:00 falls on a Tuesday

2017-01-04 00:00:00 falls on a Wednesday

2017-01-05 00:00:00 falls on a Thursday

2017-01-06 00:00:00 falls on a Friday

2017-01-07 00:00:00 falls on a Saturday

2017-01-08 00:00:00 falls on a Sunday

 


Copyright 2024 © pythontic.com