-1

I have to add a line before the code or it won't display properly

now = datetime.now()
year = now.year
month = now.month
day = now.day
hour = now.hour
minute = now.minute
second = now.second
testTime = datetime(year,month,day,hour,minute,second)
print testTime - now

The code above give me result like this :-1 day, 23:59:59.106000,but it should be only microseconds,right?

The funny thing is if I add 1 to a field,say minute,it will give me the right answer.

Does anyone know what is going on? Thanks.

冯克勤
  • 37
  • 5
  • 4
    You have them backwards. `print now - testTime` – Ouroborus Aug 17 '19 at 01:12
  • 1
    Possible duplicate of [Python timedelta issue with negative values](https://stackoverflow.com/questions/8408397/python-timedelta-issue-with-negative-values) – manveti Aug 17 '19 at 01:13

1 Answers1

0

That output is correct. It's telling you that, in order to make testTime equal to now, you'd need to subtract one day from testTime (that's the -1 day), then add back 23 hours, 59 minutes, 59 seconds, and 106000 microseconds. Effectively, that's saying "testTime is 894000 microseconds before now", which is correct; you truncated off the microseconds, makingtestTime ever-so-slightly earlier than now.

Are you sure you didn't flip your operands? print(now - testTime) would output 0:00:00.894000 which is probably the output you were looking for.

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
  • Thanks,you are totally right.And when I use print (testTime - now).total_seconds(),it give me something like -0.255.That's more readable. – 冯克勤 Aug 23 '19 at 10:46