使减法成为可能:'datetime.time'和'datetime.datetime'

时间:2016-01-30 03:24:35

标签: python

我是Salech,我正在学习Python。 Python是我第一个编程语言。这是我第二天关注youtube视频“从零到英雄”。我无法解决的第一个问题与时间和日期有关。

挑战:

  • 要求用户输入项目的截止日期
  • 告诉他们完成项目需要多少天
  • 对于额外学分,给他们答案为几周和几周的组合。天

我做了所有这些,但后来我想添加一个额外的功能,它需要输入时间(hh:mm:ss)并打印此时间减去当前时间。以下是我的想法:

import math
import datetime

currentDate = datetime.date.today()
currentTime = datetime.datetime.now()

deadLine = input('Hello, enter the deadline date for your project (mm/dd/yyyy)')
deadLineDate = datetime.datetime.strptime(deadLine, '%m/%d/%Y').date()

deadLineTime = input('insert time')
deadTime = datetime.datetime.strptime(deadLineTime, '%H:%M:%S').time()
print(deadTime)

daysLeft = deadLineDate - currentDate
print('%d days left' % daysLeft.days)


weeksLeft = math.floor(daysLeft.days/7)
newDaysLeft = daysLeft .days- 7*(math.floor(daysLeft.days/7))
print('You have %d weeks' % weeksLeft, ' and %d days left.' % newDaysLeft)

timeLeft = deadTime - currentTime 
print(timeLeft.hours)

输入02/04/2016和15:00我收到以下错误:

Hello, enter the deadline date for your project (mm/dd/yyyy)02/04/2016
insert time15:00
15:00:00
5 days left
You have 0 weeks  and 5 days left.
Traceback (most recent call last):
  File "/Users/PYTHON/challenge04.py", line 31, in <module>
    timeLeft = deadTime - currentTime
TypeError: unsupported operand type(s) for -: 'datetime.time' and 'datetime.datetime'
>>> 

编辑:正如乔纳坦所说,在没有任何输入的情况下测试代码:

Hello, enter the deadline date for your project (mm/dd/yyyy)
Traceback (most recent call last):
  File "/Users/PYTHON/challenge04.py", line 14, in <module>
    deadLineDate = datetime.datetime.strptime(deadLine, '%m/%d/%Y').date()
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/_strptime.py", line 507, in _strptime_datetime
    tt, fraction = _strptime(data_string, format)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/_strptime.py", line 344, in _strptime
    (data_string, format))
ValueError: time data '' does not match format '%m/%d/%Y'

谢谢。

2 个答案:

答案 0 :(得分:2)

您需要combinedatetime datetime改为deadline = datetime.datetime.combine(deadLineDate, deadlineTime) timeLeft = deadline - currentTime

date

错误的原因是因为从time中减去$(document).ready(function() { $(document).focus(); var coba=[]; $(document).on('keypress',function(e){ coba.push(String.fromCharCode(e.which)); if (coba[coba.length-1]=="\r") { console.log(coba.join('')); simpan(coba.join('')); coba = []; }; }); }); 并没有多大意义。例如什么是“4PM - 1月29日星期五?”。

答案 1 :(得分:0)

import datetime
import math

currentDate=datetime.date.today()
currentTime=datetime.datetime.now()

UserInput1=input("What is the deadline for your project? mm/dd/yyyy  ")
deadLine=datetime.datetime.strptime(UserInput1, "%m/%d/%Y").date()

UserInput2=input("Please insert the time hh/mm/ss  ")
deadTime=datetime.datetime.strptime(UserInput2, "%H/%M/%S").time()


daysLeft= deadLine-currentDate
print("%d days left" % daysLeft.days)

weeksLeft=math.floor(daysLeft.days/7)
newDaysLeft=daysLeft.days-7*(math.floor(daysLeft.days/7))
print("You have %d weeks" % weeksLeft, "and %d days left."% newDaysLeft)

deadLine=datetime.datetime.combine(deadLine,deadTime)
timeLeft=deadLine-currentTime
print(timeLeft)