0

I have some string dates ı try to calculate how much day have from today to string day example string dates

examples=["June 30 - July 16","Thursday, July 15","July 9 - 19"]

I write for one type

import datetime
arriveTimes="June 30 - July 16"
dates=arriveTimes.split(" - ")
monthToNumber = {
'January' : 1,         
'February' : 2,         
'March' : 3,           
'April' : 4,              
'May' : 5, 
'June' : 6,
'July' : 7, 
'August' : 8, 
'September' : 9, 
'October' : 10, 
'November' : 11, 
'December' : 12}
firstMounth=""
arriveTimesOnDayList=[]
for date in dates:
    
    month,day=date.split(" ")
    
    
    month=str([v for k, v in monthToNumber.items() if month.lower() in k.lower()][0])
    today = datetime.datetime.now()
    year=str(today.year)
    dateTimeObj = datetime.datetime.strptime(year+"-"+month+"-"+day, '%Y-%m-%d')
    arriveTimeDatetimeObj = dateTimeObj - today
    arriveTimesOnDayList.append(str(arriveTimeDatetimeObj.days))
print(arriveTimesOnDayList)
arriveTimesOnDayStr=arriveTimesOnDayList[0]+"-"+arriveTimesOnDayList[1]
print(arriveTimesOnDayStr)

but I want it support all type

8
  • 3
    What is "all type"? As in all possible date format? Commented Jun 26, 2021 at 10:50
  • for example it can be "Wednesday ,Thursday" when today is Monday Commented Jun 26, 2021 at 11:02
  • example day interest today if not letter than one week it is expressed as a days if not letter than one month and letter than one week Commented Jun 26, 2021 at 11:28
  • Your first example seems to be more a range of dates, the second one is a date, the third one maybe a range again. So how do you count the days from today when you have a range? Commented Jun 26, 2021 at 12:12
  • 1
    It's still unclear what you want, your code should calculate the time difference between today and another date. However, some of your example consist of two dates, assumed today is June 27, what is the expected result when example is June 30 - July 16? 2 or 18 or some time between? Perhaps you should add more examples to show what all type you want, and expected results for all of your examples to make it clear. By the way, I think your current code doesn't work. Commented Jun 27, 2021 at 2:22

1 Answer 1

1

datetime doesn't have the functionality to parse arbitrary date string, you may need to find some libraries doing that.

datetime.strptime() needs fixed format so you need to keep all date strings in same format, or you may need to have a list of all formats you want, then try them one by one.

from datetime import datetime
def get_date(date):
    # %B for month full name, %A for weekday full name
    formats = ["%B %d", "%A, %B %d", "%d"]
    today = datetime.now()
    for f in formats:
        try:
            dt = datetime.strptime(f"{today.year} {date}", f"%Y {f}")
            # if the date is before today, treated as date in next month
            if dt < today:
                dt = dt.replace(month=today.month+1)
            return dt
        except ValueError:
            continue
    else:
        print("Date format not supported.")

def day_from_today(dates):
    for date in dates:
        dt = get_date(date)
        if dt:
            dft = (dt - datetime.now()).days
            print(f"{dft} {'days' if dft > 1 else 'day'} to {dt.date()}")

example = ["June 30", "July 16", "Thursday, July 15", "June 28", "19", "7 19"]
day_from_today(example)

Result:

2 days to 2021-06-30
18 days to 2021-07-16
17 days to 2021-07-15
0 day to 2021-06-28
21 days to 2021-07-19
Date format not supported.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.