Search code examples
python-3.xdatetimepython-datetimepython-dateutil

Get day name from starting date


I developing a course app. In my app, if a student register for a new course, the teacher must input the starting course date for the student.

And in this app, the student can pick up the day that they want to learn to the teacher. e.g: The student wants to learn 3 days for each week, Monday, Tuesday and Friday.

And for the day that the student taking, they must pay to the teacher on that day.

So, I want my app can automatically display on what day a student must pay the tuition to the teacher.

The thing that I need here is: if the teacher inputted the starting course date on 20-10-2019 (%d-m-Y%), and the days that the student taking are Monday, Tuesday and Friday, I want to print: print('please, pay it') every week on the selected day.

enter image description here

So, I want something like this:

import datetime

starting_course_date = datetime.datetime.strptime("20-10-2019", "%d-%m-%Y")

student_taking_days = [{'day': 'Monday'}, {'day': 'Tuesday'}, {'day': 'Friday'}]
today = datetime.datetime.utcnow()

if today > starting_course_date and today is one of day on the student_taking_days:
    print('please, pay it')

Please, any answer, source or refer tutorial will be very appreciated :)


Solution

  • You can get weekday of your today. More info here

    import datetime
    weekday = datetime.datetime.today().weekday()
    if (weekday in [0, 1, 4]): // 0 is monday, 1 is tuesday and 4 is friday
        print('please, pay it')
    

    You event can get day name like this using strftime:

    weekday_name = weekday.strftime("%A")
    if (weekday_name in ['Monday', 'Tuesday', 'Friday']):
        print('please, pay it')