0

enter image description heredef main():

date_tokens = date_input.split('/')
day = int(date_tokens[0])
month = int(date_tokens[1])
year = int(date_tokens[2])

if 1/1/1990<=31/12/2020 : date_input = input('Date: ') else: int(input("The date that you enter is not valid, please input a date in between 1/1/1990 and 31/12/1990 "))

main()

  • can you post the code in a better way? – Juan Benitez May 26 '20 at 10:47
  • Hi Geeshani Liyanage - welcome at Stack Overflow. Please let us know what you want to achieve and what is the problem with the code that you have. – Amos Egel May 26 '20 at 10:53
  • 1
    Does this answer your question? [How to check if the current time is in range in python?](https://stackoverflow.com/questions/10747974/how-to-check-if-the-current-time-is-in-range-in-python) – Nathan May 26 '20 at 10:53

2 Answers2

0

The whole code would be:

from datetime import date


date_tokens = date_input.split("/")

if date(1990,1,1) <= date(int(date_tokens[0]), int(date_tokens[1]), int(date_tokens[2])) <= date(2020,12,31):
    date_input = input('Date: ')
else:
    input("The date that you enter is not valid, please input a date in between 1/1/1990 and 31/12/1990 ")
  • Could you expand on the reasons why this works to solve the issue? – Marshall Davis May 26 '20 at 18:19
  • Other than a extra dot and 1900 instead of 1990, why wouldn't it solve the issue? I wrote the whole code. It wokrs on my machine :) – Giorgos Kavalieratos May 26 '20 at 20:12
  • I was referring to the explanation and documentation to help users find information on how it works and further research. Much like the accepted answer includes, this is helpful for others who may find this question similar and the answer help solve their problem. – Marshall Davis May 27 '20 at 18:20
0

python's datetime object is your friend: Datetime Documentation

It allows you to easily make calculations on dates, including comparisons like you want.

import datetime

date_input = datetime.date(year, month, day)
if date_input > datetime.date(1990, 1, 1) and date_input < datetime.date(1990, 12, 31):
    date_input = input('Date: ')
else:
    int(input("The date that you enter is not valid, please input a date in between 1/1/1990 and 31/12/1990 "))

Also, if you want user to keep entering dates you may want to do that inside a loop or of course put the input inside date_input:

def main():
    date_input = input ('Date: ')
    date_tokens = date_input.split('/')
    day = int(date_tokens[0])
    month = int(date_tokens[1])
    year = int(date_tokens[2])
    date_input = datetime.date(year, month, day)
    if date_input > datetime.date(1990, 1, 1) and date_input < datetime.date(1990, 12, 31):
        date_input = input('Date: ')
    else:
        int(input("The date that you enter is not valid, please input a date in between 1/1/1990 and 31/12/1990 "))
        main()


main()

(I got a bit lazy so I just called main() again. Same result expect for another "Date: " print)

Roim
  • 2,986
  • 2
  • 10
  • 25