Bash----
Bash----

Reputation: 95

Finding closest date to today's date from a dictionary

I am wanting to find the number of days between today's date and the next date in a dictionary of dates. Firstly, i am trying to find the closest date in the dictionary to the current date.

current_date = datetime.today()
date_schedule = {
    "1": "2019-7-29",
    "2": "2019-8-27",
    "3": "2019-9-27",
    "4": "2019-10-28",
    "5": "2019-11-27",
    "6": "2019-12-27",
    "7": "2020-1-27",
    "8": "2020-2-27",
    "9": "2020-3-27",
    "10": "2020-4-27",
    "11": "2020-5-27",
    "12": "2020-6-29",
}

def days_till_payment(date_schedule, current_date):
    days_left = min(date_schedule, key=lambda x: abs(x - current_date))
    return days_left

However, when i try and run this, i get the following error:

TypeError: unsupported operand type(s) for -: 'str' and 'datetime.datetime'

I would really appreciate someone please explain to me how I am able to get around this and find how many days are in between the current date and the next closest date from the dictionary.

EDIT: This function would be better if it returned the NEXT UPCOMING date, rather than just the nearest date (which includes dates already gone). I would like to output the next upcoming date & how many days there are until this date from today.

Upvotes: 2

Views: 1024

Answers (3)

han solo
han solo

Reputation: 6590

You need to convert the str into datetime object. Then,

1.Find the mindate from date_schedules where mindate >= current date

2.Find the days difference between mindate - current date

from datetime import datetime

date_schedule = {
    "1": "2019-7-29",
    "2": "2019-8-27",
    "3": "2019-9-27",
    "4": "2019-10-28",
    "5": "2019-11-27",
    "6": "2019-12-27",
    "7": "2020-1-27",
    "8": "2020-2-27",
    "9": "2020-3-27",
    "10": "2020-4-27",
    "11": "2020-5-27",
    "12": "2020-6-29",
}

def keyfunc(date):
    return (date - datetime.today()).days

def days_till_payment(date_schedule, today):
    dates = [datetime.strptime(v, '%Y-%m-%d') for v in date_schedule.values()]
    search_dates = [date for date in dates if date >= today]
    date = min(search_dates, key=keyfunc)
    return (date - today).days, date.strftime('%Y-%m-%d')


>>> days, date = days_till_payment(date_schedule, datetime.today())
>>> days
19
>>> date
'2019-10-28'

Upvotes: 2

Charif DZ
Charif DZ

Reputation: 14721

Using map, filter, min the benefits of this, the date is converted only one time, we loop over the date list only one time:

from datetime import datetime

date_schedule = {
    "1": "2019-7-29",
    "2": "2019-8-27",
    "3": "2019-9-27",
    "4": "2019-10-28",
    "5": "2019-11-27",
    "6": "2019-12-27",
    "7": "2020-1-27",
    "8": "2020-2-27",
    "9": "2020-3-27",
    "10": "2020-4-27",
    "11": "2020-5-27",
    "12": "2020-6-29",
}


def days_till_payment(date_schedule, current_date):
    converted_data = map(lambda v: datetime.strptime(v, '%Y-%m-%d'),  date_schedule.values() )
    filtered_data = filter(lambda v: v >= current_date, converted_data)
    min_date = min(filtered_data, key=lambda v: v - current_date)
    return min_date.strftime('%Y-%m-%d')


current_date = datetime.today()
print(days_till_payment(date_schedule, current_date)) # 2019-10-28

Upvotes: 1

stovfl
stovfl

Reputation: 15513

Question: I would like to output the next upcoming date & how many days there are until this date from today.


Note: The dict items have to be in ordered sequence. Use collections.OrderedDict instead!


def strptime(datestr):
    return datetime.strptime(datestr, '%Y-%m-%d')

def days_till_payment(date_schedule, current_date):
    for k, v in date_schedule.items():
        v = strptime(v)
        if v > current_date:
            return v,  (v - current_date).days

print(days_till_payment(date_schedule, datetime.now()))

>>> (datetime.datetime(2019, 10, 28, 0, 0), 19)

Upvotes: 0

Related Questions