Class claendar Python

-1

I need to make a python program that by a date, specifically on February 19, 2020. get what day of the week will fall, it is important that it is through the use of the class calendar, thanks to antemeno.

    
asked by user72608 22.11.2018 в 08:52
source

1 answer

2

calendar is not the appropriate tool for it, at least not directly. The calendar module is used to generate months or full year calendars (in text or html) and not for a particular day.

Although you could use calendar.Calendar().monthdatescalendar(2020,2) to get a list with the weeks of February 2020, each of those weeks will be another list whose elements are objects datetime.date . One of those elements will be the day you are looking for, datetime.date(2020,2,19) , from which you could take the day of the week with weekday() , but for that why not directly use datetime.date(2020,2,19) without needing to enter calendar of by , that only complicates things?

This would be the solution without using calendar :

from datetime import date
print("Dia de la semana:", date(2020, 2, 19).weekday())

You would get that the day of the week is the 2, and knowing that the 0 is Monday that implies that the day in question is Wednesday.

If you want the day of the week to come out by name, instead of number, you can prepare a list with the names:

nombres_dias_semana = [ 'lunes', 'martes', 'miércoles', 'jueves', 'viernes', 'sábado', 'domingo' ]
fecha = date(2020, 2, 19)
print("El {} es {}.".format(fecha,  nombres_dias_semana[fecha.weekday()]))
El 2020-02-19 es miércoles.

Using calendar , what you can easily get is a calendar of the month, like this:

>>> from calendar import TextCalendar
>>> TextCalendar().prmonth(2020,2)
   February 2020
Mo Tu We Th Fr Sa Su
                1  2
 3  4  5  6  7  8  9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29

But getting the day of the week off a particular day is much more complex.

The only utility that can have the calendar module in all this, is that you can avoid defining your list with the names of the days of the week, because the module brings a defined one in calendar.day_name that you could use instead of nombres_dias_semana . The list of calendar also adapts to the language you have set in locale , which may be convenient if the application has to be multilingual.

If this is not the solution you were looking for, edit the question to add more details and especially the code you have done so far using calendar .

    
answered by 22.11.2018 / 10:21
source