计算用户输入的2个日期之间的剩余天数

时间:2017-10-10 06:57:44

标签: python datetime time

我正在尝试计算用户输入日期但无法找到日期的两个日期之间的天数。我目前有这个代码:

from datetime import date

startdate = date(input("When do you intend to start?"))
enddate = date(input("When is your deadline?"))
numstudydays = enddate - startdate
print (numstudydays)

谢谢

3 个答案:

答案 0 :(得分:1)

在python中,input()方法接受输入作为字符串变量。然后,您可以使用int()方法将其类型转换为整数。但是你无法对整个元组进行类型转换。即你的情况(年,月,日)。 您可以通过逐个获取所有值来实现。

from datetime import date

print("Enter intended start date details: ")
year = int(input('Year: '))
month = int(input('Month: '))
day = int(input('Day: '))
startdate = date(year, month, day)

print("Enter intended enddate details: ")
year = int(input('Year: '))
month = int(input('Month: '))
day = int(input('Day: '))
enddate = date(year, month, day)
numstudydays = enddate - startdate

print (numstudydays)

希望有所帮助。

答案 1 :(得分:0)

我认为您最大的问题是如何检索日期。 date()函数需要三个整数,例如日期(2017,10,10)

当你使用input()时,你得到一个字符串:

input()  #returns a string

你还需要通过以某种方式分割输入来解决这个问题:

e.g。

"2017-10-12".split("-") # returns ["2017","10","12"]
[int(i) for i in "2017-10-12".split("-")] # returns [2017,10,12]

这意味着如果你这样做,你会得到一个日期:

date(*[int(i) for i in input("When is your deadline?").split("-")])

但在这种情况下你真正想要的是使用strptime。 Strptime()通过定义字符串的外观将字符串转换为日期时间:

from datetime import datetime
datetime.strptime("20171012","%Y%m%d").date() # returns: datetime.date(2017, 10, 12)

完整脚本示例

from datetime import datetime

startdate_str = input("When do you intend to start? e.g.(2017-10-10)")
enddate_str = input("When is your deadline? e.g.(2017-10-11)")

startdate_dt = datetime.strptime(startdate_str,"%Y-%m-%d").date()
enddate_dt = datetime.strptime(enddate_str,"%Y-%m-%d").date()

numstudydays = enddate_dt - startdate_dt

print (numstudydays.days) # prints 1 for example inputs

推荐链接

格式化字符串的各种方法: https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior

答案 2 :(得分:0)

您正在错误地使用datetime构造函数。我修改了您的解决方案,以简单易用的方式处理用户输入。基本数据验证由datetime.strptime factory method执行,但可能会有更多(例如,检查结束日期总是在开始日期之后):

from datetime import datetime, date

start, end = None, None

while start is None:
    rawinput = input("When do you intend to start? ")

    try:
        # %Y stands for a fully specified year, such as 2017, 1994, etc.
        # %m for the integer representation of a month, e.g. 01, 06, 12
        # and %d for the day of the month
        # see https://docs.python.org/3/library/time.html#time.strftime for more
        start = datetime.strptime(rawinput, "%Y %m %d")
    except ValueError:
        print("Input must be \"<year number> <month number> <day number>\"")

while end is None:
    rawinput = input("When is your deadline? ")

    try:
        end = datetime.strptime(rawinput, "%Y %m %d")
    except ValueError:
        print("Input must be \"<year number> <month number> <day number>\"")

diff = end - start

print("You have %d days of time" % diff.days)

我做了不止一些测试,结果就是这样:

None@vacuum:~$ python3.6 ./test.py 
When do you intend to start? 2017 01 32
Input must be "<year number> <month number> <day number>"
When do you intend to start? 2017 01 31
When is your deadline? 2017 02 30
Input must be "<year number> <month number> <day number>"
When is your deadline? 2017 02 28
You have 28 days of time
相关问题