2014-11-01 244 views
-2
from datetime import date 
future = input("Enter a date(dd/mm/yyyy): ") 
daystring = future[0:2] 
monthstring = future[3:5] 
yearstring = future[6:10] 

today = (date.today()) 
month = date.today().month 
year = date.today().year 
day = date.today().day 

if monthstring == "01" or "03" or "05" or "07" or "08" or "10" or "12": 
    if daystring > "31": 
     print("Invalid Date Entered") 
if monthstring == "04" or "06" or "09" or "11": 
    if daystring > "30": 
     print("Invalid Date Entered") 
months = ["Jan", "Feb", "Mar", "Apr", "May", "June", 
      "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"] 
daysinmonth = [31, 29, 31, 30, 31, 30, 31, 31, 30, 
       31, 30, 31] 


if future < today or monthstring > "12": 
    print("Invalid Date Entered") 

else: 
    layout = "%d/%m/%Y" 
    a = datetime.strptime(future, layout) 
    b = datetime.strptime(today, layout) 
    delta = a - b 
    print ("The difference between the inputted date and todays date is: ",delta.days, "days") 

此代码是要求用户在将来输入日期,然后代码应该使用该输入并从中减去当前日期。两个日期之间的差异python

例如,今天是01/11/2014,如果用户输入03/11/2014,则输出应该是差值为2天。

但是,每次输入未来日期时都会出现错误。

+1

* “我得到一个错误每次我输入未来的日期” * - **什么错误**如果这是Python 2.x,你应该使用'raw_input'。为什么你从'datetime'导入''然后手动切割'future'并尝试自己解析它?只需*使用'strptime' *。 – jonrsharpe 2014-11-01 16:06:15

+1

你在比较字符串;你知道当你这样做时,'9'>'31'是真的吗? – 2014-11-01 16:07:10

+0

我使用python 3 – user3396351 2014-11-01 16:07:29

回答

-1

有不同的事情需要考虑。尝试考虑这些要点

  • 检查用户是否输入正确的日期时间。你可以使用正则表达式来做到这一点。您可以使用RE模块。
mat=re.match('(\d{2})[/](\d{2})[/](\d{4})$', str(future)) 
  • 你应该几个月过滤1-12之间的值,并1-31天之间。然后区分月份,并且在两种情况下都不要忘记February(闰年或否)。您可以使用calender模块来检查。
t= calendar.monthrange(int(yearstring), 2) # This is to check if February has 29 or 28 (is leap year or no), you can do that by checking the day number of "datetime.date (int(yearstring), 3, 1) - datetime.timedelta (days = 1)" 
  • 然后,两个日期之间的比较,你应该使用正确的时间格式为每日期。
a = date.datetime.strptime(future, "%d/%m/%Y").date() 
b = date.datetime.strptime(today, "%Y-%m-%d").date() 
delta = a-b 

你的代码是这样:?!

import datetime as date 
import re 
import calendar 

future = input("Enter a date(dd/mm/yyyy): ") 
#eg: future="02/03/2015" 
#print future 

mat=re.match('(\d{2})[/](\d{2})[/](\d{4})$', str(future)) 
if mat is None: 
    print("Invalid Date Entered, try to enter a date in this form(dd/mm/yyyy) ") 
else: 
    daystring,monthstring,yearstring= mat.groups() 

    today= str(date.datetime.now()).split(" ")[0] 
    year, month, day = today.split("-") 

    months = ["Jan", "Feb", "Mar", "Apr", "May", "June","Jul", "Aug", "Sep", "Oct", "Nov", "Dec"] 
    daysinmonth = [31, 29, 31, 30, 31, 30, 31, 31, 30,31, 30, 31] 

    if int(daystring)>0 and int(daystring)<=31 and int(monthstring)>0 and int(monthstring) <= 12 and int(yearstring)>0: 
     if monthstring in ["01", "03", "05" ,"07","08","10", "12"]: 
      if int(daystring) > 31: 
       print("Invalid Date Entered - Number of days can't exceed 31.") 
     if monthstring in ["04","06","09","11"]: 
      if int(daystring) > 30: 
       print("Invalid Date Entered - Number of days can't exceed 31.") 
     if monthstring == "02": 
      t= calendar.monthrange(int(yearstring), 2) # This is to check if February has 29 or 28 (is leap year or no), you can do that by checking the day number of "datetime.date (int(yearstring), 3, 1) - datetime.timedelta (days = 1)" 
      if int(daystring) !=t: #You forget faberuary 
       print("Invalid Date Entered - of days can't exceed 28 or 29") 


     a = date.datetime.strptime(future, "%d/%m/%Y").date() 
     b = date.datetime.strptime(today, "%Y-%m-%d").date() 
     delta = a-b # The future - Today ::> to get a positive number 
     if delta.days >0: 
      print ("The difference between the inputted date and todays date is: ",delta.days, "days") 
     else: 
      print("Date is in the past") 

    else: 
     print("Invalid Date Entered") 
+0

非常感谢您的帮助 – user3396351 2014-11-01 20:32:23

+0

有**绝对没有必要**为此。你为什么要手动检查日期?只要用'try'来防止'strptime'的呼叫,并让图书馆做好工作。 – jonrsharpe 2014-11-02 09:17:07

0
import dateutil.parser as parser 
import datetime 
date1 = parser.parse(input("Enter Date:")) 
print((datetime.datetime.now() - date1).days) 

dateutil是在分析自然日期字符串很不错...

你可能需要安装dateutil

easy_install python-dateutil 
如果你想不使用dateutil提到你可以使用strptime

def get_user_date(prompt="Enter Date:"): 
    while True: 
     try: 
      return datetime.datetime.strptime("%m/%d/%y",input(prompt)) 
     except ValueError: 
      print("Invalid Input!") 

这段代码我认为可以在Python 3中工作