首页 > 解决方案 > 从输入中取一个日期,看看它是从今天开始的天数

问题描述

我正在尝试制作一个程序,该程序采用用户以“yyyy mm dd”格式输入的日期,并查看该输入从今天开始的天数。我不知道该怎么做,但如果你能提供帮助,那就太好了。

    input("This is a function to find the difference between today and a date 
    you enter. Enter the date in this format: 'yyyy mm dd' ") 

标签: python

解决方案


这应该工作

import datetime
today = datetime.date.today()
year = int(input('Enter a year'))  #input the needed year
month = int(input('Enter a month')) #input the needed month
day = int(input('Enter a day'))  #input the needed day
neededdate = datetime.date(year, month, day)
days_remaining = neededdate - today
print(days_remaining.days)

我编辑输入日期、月份和年份。


推荐阅读