Python Forum
Would like to input a date variable and determine whether it is within the time range
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Would like to input a date variable and determine whether it is within the time range
#1
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!
Reply
#2
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.
Reply
#3
Noted on the BBCode Gribouillis, however the program is still unable to work.
Reply
#4
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: ")
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Compare current date on calendar with date format file name Fioravanti 1 205 Mar-26-2024, 08:23 AM
Last Post: Pedroski55
  Date Time Series Help...Please spra8560 2 351 Feb-01-2024, 01:38 PM
Last Post: spra8560
  Python date format changes to date & time 1418 4 576 Jan-20-2024, 04:45 AM
Last Post: 1418
  Downloading time zone aware files, getting wrong files(by date))s tester_V 9 1,015 Jul-23-2023, 08:32 AM
Last Post: deanhystad
  Formatting a date time string read from a csv file DosAtPython 5 1,242 Jun-19-2023, 02:12 PM
Last Post: DosAtPython
  input variable choice MCL169 7 1,156 Feb-19-2023, 09:00 PM
Last Post: MCL169
  Wait til a date and time KatManDEW 2 1,413 Mar-11-2022, 08:05 PM
Last Post: KatManDEW
  matplotlib x axis range goes over the set range Pedroski55 5 3,166 Nov-21-2021, 08:40 AM
Last Post: paul18fr
  Date format and past date check function Turtle 5 4,211 Oct-22-2021, 09:45 PM
Last Post: deanhystad
  How to include input as part of variable name Mark17 4 2,489 Oct-01-2021, 06:45 PM
Last Post: Mark17

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020