Skip to content Skip to sidebar Skip to footer

Issue Writing A Function Which Determines If A Date Is In The Future

I'm trying to to define a function called inTheFuture() that accepts a given year number, a month number, and a day number as 3 separate arguments. The function should return a Boo

Solution 1:

To work with dates (without time), you could use datetime.date class:

#!/usr/bin/env python3from datetime import date

defin_future(date_to_test):
    """Whether *date_to_test* is in the future."""return date_to_test > date.today()

input_date = date(*map(int, input("Enter Year-Month-Day: ").split('-')))
print("Got {}. Is it in the future?".format(input_date))
print("yup"if in_future(input_date) else"nope")

Solution 2:

It's exactly as the error says: you attempt to compare float(ymd) to today, where ymd was assigned the tuple (year, month, day). Exactly what result were you expecting? What do you think should happen when you pass (year, month, day) to float? It does not represent a float in any way I can think of.

But even then, your comparison makes no sense, because today is a datetime.datetime instance. What do you expect to happen when you compare a float to that? How is that supposed to work?

Compare apples to apples. If you want to know whether the date and time represented by a given year, month and day value is before a given datetime.datetime value, then use the year, month and day to create the kind of thing that actually represents a date and time - i.e., a datetime.datetime instance - the same kind of thing that you're comparing it to. Apples to apples.

The built-in help, as well as the online documentation, explain that this is a simple matter of passing those values, in the year/month/day order, to the datetime.datetime constructor:

day= datetime.datetime(year, month, day)
if day> today: # etc.

Incidentally, there are multiple other issues with your program. You should not explicitly raise SystemExit yourself to abort a script (and it's generally better to let it just end naturally) - this is what exit() is for. You attempt to compute inf before you've actually verified the year, month and day values (but actually, there is no need to do this yourself; instead, the attempt to create a datetime.datetime with invalid values will raise a ValueError that you can detect for yourself). You have semicolons everywhere, which are unnecessary and a bad habit in Python. The boolean constants in Python are spelled True and False, with capital letters. Finally, your last line simply refers to inf, but doesn't do anything with it; this will not cause a value to be displayed. Within a script, you need to explicitly print things that you want to be output.

Solution 3:

You can't convert a date to a float, a string or an int. date implements it's own comparisons. You need to convert your arguments to comply. This should work:

today = datetime.datetime.today().date()

def inTheFuture(year, month, day):
    ymd=datetime.datetime(year,month,day).date()
    if ymd > today:
        returnTrueelse:
        returnFalse

Post a Comment for "Issue Writing A Function Which Determines If A Date Is In The Future"