mistake comparing datetime, python
i wrote this code import time import datetime import os import msvcrt as m while True: try: os.system("cls") delivery = input("Insert delivery date (dd/mm/aaaa): ") dateDelivery = datetime.datetime.strptime(delivery, "%d%m%Y").strftime("%d/%m/%Y") dateInput = time.strftime("%d/%m/%Y") except ValueError: print("\nWrong format\n\nTry again") m.getch() continue if dateDelivery <= dateInput: print("\nThe inserted date has already past\n\n", dateDelivery, "\n\nla Actual date: ", dateInput) m.getch() continue else: break print("Input date: ", dateInput, "\nDelivery date: ", dateDelivery) It needs to compare 2 dates: a date to send a delivery and the actual date, so if te date to send the delivery is a past date, the program send a message indicating that. The program works fine, but if i insert some dates in future, the program recognize them as a past dates. Try with 12122016. I'm very newbie at code, so i think this is a very simple mistake. Sorry about my english, i'm trying to improve it here too thanks to all
You're comparing between two dates converted to string type, so the comparation is lexicographic. You should use datetime type like this: dateDelivery = datetime.datetime.strptime(delivery,"%d%m%Y") dateInput = datetime.datetime.now() You can see the difference by printing the type of the variables: print(type(dateDelivery)) print(type(dateInput)) before and after the change.
Every time you obtain a datetime value, you covert it to a string using strftime("%d/%m/%Y"). So when you compare dateDelivery with dateInput, you are comparing two pieces of text, not datetime values. When using a dd-mm-yyyy format, the ordering of text does not always correspond to datetime ordering (if you really wanted to, you could use a yyyy-mm-dd format, which has correct ordering). For example: >>> "01/01/2015" <= "01/01/2016" True >>> "02/01/2015" <= "01/01/2016" # wrong? False So when asking before comparing the dates, you should not convert them to strings. dateDelivery = datetime.datetime.strptime(delivery, "%d%m%Y") dateInput = datetime.datetime.now() Then, when showing the results to the user, you can convert them to strings: humanDateInput = dateInput.strftime("%d/%m/%Y") humanDateDelivery = dateDelivery.strftime("%d/%m/%Y") print("Input date:", humanDateInput) print("Delivery date:", humanDateDelivery)
Django filter against ForeignKey and by result of manytomany sub query
How to check if a date time is before midday
Merge rows based on values and key string in awk, sed or python
i find the files at the end empty ,python
How do I prevent my FOR loop from ending too early?
PyInstaller 2.1 import custom package
Python: Read File and append a lot of list of 28 lines
Numba Matrix Vector multiplication
How to improve very inefficient numpy code for calculating correlation
Flask-strange routing issue
Upgrade path for re-usable apps with South AND django 1.7 migrations
Making scikit-learn train on all training data after cross-validation
matplotlib does not display latex fonts correctly
How to use py2exe with third-party libraries
How to extract two numbers from input line and tab delimit them?
Copy selected lines of text from one file to another with command line argument with or without spaces