Is there a date/year function in python?

Viewed 41

how do i write a code that notifies me that a certificate is valid from 17 March 2017 to 16 March 2022 and should be renewed from 17 March 2022 for another validity period of 5 years. Please note that the standard validity period is usually 5 years.

I put together some code on jupyter notebook using python which I ran and got an output as shown in the attached image, I however feel I however know it has not been done right.

find my codes below:

code sample used

2 Answers

Note that you should post your code as code sample and not as a picture so people have more time to solve your problem than reconstructing your problem.

A solution could be datetime and relativedelta.

import datetime
from dateutil.relativedelta import relativedelta
def time_in_range(start, end, current):

    if start <= current <= end:
        print("Ticket valid")
    else:
        print("Ticket invalid")


start = datetime.date(2015, 1, 30)
end = start + relativedelta(years=5)
current = datetime.datetime.now().date()

This way you can check if a date is in a given range.

Use datetime function of python. Read this Document for details.

Related