Python Forum
Would like to input a date variable and determine whether it is within the time range - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Would like to input a date variable and determine whether it is within the time range (/thread-15119.html)



Would like to input a date variable and determine whether it is within the time range - harold - Jan-05-2019

Hi, i am new to programming. I would like to create a program which i could input a date variable and if the date variable is not within the range, it would produce another result.

import datetime
d1 = datetime.date(2019,1,1)
d2 = datetime.date(2019,1,30)
inputd_v = datetime.date("Please input date: ")
if inputd_v > d1 < d2:
    print (d1)
else:
    print ("Your date is out of range")
Please assist as right this program. Thanks!


RE: Would like to input a date variable and determine whether it is within the time range - Gribouillis - Jan-05-2019

If you can use an external library, I'd suggest dateutil
>>> from dateutil.parser import parse
>>> parse("2019 1 5").date()
datetime.date(2019, 1, 5)
>>> parse("2019 jan 5").date()
datetime.date(2019, 1, 5)
>>> parse("2019 5 january").date()
datetime.date(2019, 1, 5)
>>> parse("january 5").date()
datetime.date(2019, 1, 5)
Also don't write if inputd_v > d1 < d2:, write if d1 < inputd_v < d2. inputd_v needs to be a date instance, not a string.


RE: Would like to input a date variable and determine whether it is within the time range - harold - Jan-05-2019

Noted on the BBCode Gribouillis, however the program is still unable to work.


RE: Would like to input a date variable and determine whether it is within the time range - Gribouillis - Jan-05-2019

Quote: however the program is still unable to work.
What does python say? do you have an exception traceback? Oh I see, you forgot to use input, input("Please input date: ")