Jackie Cheng
Jackie Cheng

Reputation: 9

python- comparing date

I am writing a python assignment and we are writing a hotel's customers' rooms reservations. And we are supposed to compare the date of arriving and depart. if the depart date is earlier than the arriving date, it will print error messages but i kept getting error after i run it. I don't know why....i am trying to make the namedtuple of that customer information which is

Reservation=namedtuple('Reservation','confirmation room_number arrive_date depart_date name)

so i took the arrive_date out (10/13/2008) and because it is a string so i split it. And i tried to make all the number together as a int. After that......i use the datetime.......yeah.....it seems like i am doing something wrong here. I don't understand the meaning of error: Invalid literal for int() with base 10

This is my program:

    elif lst[0]=='LR':
        for r in new_reservation_list:
            for d in r.arrive_date:
                lsst=d.split()
                month1=lsst[:2]
                months=int(''.join([str(x) for x in month1]))
                date1=lsst[3:5]
                dates=int(''.join(([str(x) for x in date1])))
                year1=lsst[7:]
                years=int(''.join(([str(x) for x in year1])))
            arrive_date1=datetime.date(int(months),int(dates),int(years))
            for e in r.depart_date:
                lstt=e.split()
                month2=lsst[:2]
                months2=int(''.join([str(x) for x in month2]))
                date2=lsst[3:5]
                dates2=int(''.join(([str(x) for x in date2])))
                year2=lsst[7:]
                years2=int(''.join(([str(x) for x in year2])))
            depart_date1=datetime.date(int(months2),int(dates2),int(years2))
            if arrive_date>depart_date==True:
                print('Sorry, can''t reserve room', r.room_number,' (',r.arrive_date,' to ',r.depart_date,'); can''t leave before you arrive.')
            elif lst[1] in new_reservation_list:
                    print('Sorry, can''t reserve room ',r.room_number, ' (',r.arrive_date,' to ',r.depart_date,'); it''s already booked (Conf. #',r.confirmation_number)
            else:
                print('Reserving room',r.room_number,'for',r.name,'-- Confirmation #',r.confirmation_number,'\n       ','arriving',r.arrive_date,', departing ',r.depart_date)
                print('{:5}{:8}{:16}{:15}{:25}'.format('No.','Rm.','Arrive','Depart','Guest'))
                print('{:5}{:4}{:12}{:12}{:17}'.format(r.confirmation_number,r.room_number,r.arrive_date,r.depart_date, r.name))

Upvotes: 0

Views: 327

Answers (1)

Ankit Jaiswal
Ankit Jaiswal

Reputation: 23427

Check strptime function in time module. You can convert your date using this function and compare them.

Upvotes: 2

Related Questions