%d %m %y dont match my dates - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: %d %m %y dont match my dates (/thread-15558.html) |
%d %m %y dont match my dates - PythonSnake - Jan-22-2019 Ive made a code and i need help i have python 3.4 and i made a game using a guide that said to use python 3.6 heres my code it says that %d %m %y dont match my dates from tkinter import Tk, Canvas from datetime import date, datetime def get_events(): list_events = [] with open('events.txt') as file: for line in file: line = line.rstrip('\n') current_event = line.split(',') event_date = datetime.strptime(current_event[1], '%d %m %y').date() current_event[1] = event_date list_events.append(current_event) return list_events def days_between_dates(date1, date2): time_between = str(date1 - date2) number_of_days = time_between.split(' ') return number_of_days[0] root = Tk() c = Canvas(root, width=800, height=800, bg='black') c.pack() c.create_text(100, 50, anchor='w', fill='white',\ font='Arial 28 bold underline', text='My Countdown Calender') events = get_events() today = date.today() vertical_space = 100 for events in events: event_name = event[0] days_until = days_between_dates(event[1], today) display = 'it is %s days until %s' % (days_until, event_name) c.create_text(100, vertical_space, anchor='w', fill='lightblue', font='Arial 28 bold', text=display) vertical_space = vertical_space + 30 RE: %d %m %y dont match my dates - metulburr - Jan-22-2019 '%d %m %y' is the format you are saying the date is in, in the file events.txt.In this case you are saying the dates format is 21 01 19 more info here https://www.journaldev.com/23365/python-string-to-datetime-strptime RE: %d %m %y dont match my dates - PythonSnake - Jan-22-2019 thank you so much that helped alot RE: %d %m %y dont match my dates - PythonSnake - Jan-23-2019 thanks for the help but after i did it, it showed me an error message saying: Traceback (most recent call last): File "C:\Python34\Callender.py", line 29, in <module> event_name = event[0] NameError: name 'event' is not defined so if anyone can would you plz help me? RE: %d %m %y dont match my dates - metulburr - Jan-23-2019 Quote:im assuming you meantfor events in events: event_name = event[0] for event in events: event_name = event[0] RE: %d %m %y dont match my dates - joe_momma - Jan-25-2019 I used 28 01 19 in the events.txt, and put in a print(events) line the out put is: [[datetime.date(2019, 1, 28)]]. A list of a list datetime.date object. Then changed the end of your script events = get_events() today = date.today() print(events[0][0]) print(today.isoformat()) myEvents= events[0][0].timetuple() print(myEvents[0]) vertical_space = 100 days_until = days_between_dates(events[0][0], today) days_until = days_between_dates(events[0][0], today) display = 'it is %s days until %s' % (days_until, myEvents[0]) c.create_text(100, vertical_space, anchor='w', fill='lightblue', font='Arial 28 bold', text=display) vertical_space = vertical_space + 30 |