isoweekday() es una función que devuelve un número entero que indica que cae la fecha dada. El número entero que devuelve representa un día según la tabla que se muestra a continuación.
Sintaxis: datetime.isoweekday()
Valor devuelto: un número entero en el rango de [1,7]
Entero devuelto | Día de la semana |
---|---|
1 | Lunes |
2 | martes |
3 | miércoles |
4 | jueves |
5 | Viernes |
6 | sábado |
7 | Domingo |
Ejemplo 1: este programa toma la fecha usando el módulo DateTime y le dice el día de la fecha.
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])
Producción:
Current Date is : 2021-08-18 The date 2021-08-18 falls on Wednesday
Ejemplo 2: En este ejemplo, tomaremos la entrada del usuario para una fecha y devolveremos el día en que cae.
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])
Producción:
The given date 2020-09-18 falls on Friday
Ejemplo 3: En este ejemplo, tomaremos la entrada del usuario para la fecha y el día en que cae y devolveremos si eso es cierto o no
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)
Producción:
Sí, su fecha dada 2021-01-01 cae en su día esperado, es decir, viernes