2

So I am trying to create a 'planner' or diary. I am trying to create the code so it advises you how much time you have left in the day before you go to bed or before you have to do an activity you have planned.

late = timedelta(hours=23)
current_time = now.hour,now.minute
time_left = (late - current_time).minutes
print 'So if the time is %s:%s, That means you have %s minutes left' % (now.hour,now.minute,time_left) 

From looking at other threads this was my approach but I have tried several other methods like

late = datetime(datetime.day,datetime.month,datetime.year,23,00) 

Again saw this on other threads. I have tried so many methods now I have begun to confuse myself with how to solve it.

Thanks again for any help in advance

EDIT: when using

late = datetime(datetime.day,datetime,month,datetime.year,23,00) 

I got the Valuerror 'day is out of range month'

and with the current code

late = timedelta(23,00) 

I got a typeerror 'unsupported operand type(s) for -: 'datetime.timedelta' and 'tuple' In this example.

I just want the code to output the time difference between 11:00pm (or 23:00) and the current time in minutes. Sorry for not being clearer

ANSWERED EDIT: Thank you to the comments and the answer the problem I was making the code to complicated and not using datetime() correctly,

    y = today.year
    m = today.month
    d = today.day

    late = datetime(y,m,d,23,0,0)

I had the year month and day the wrong way round and had the format for the time wrong.

Thanks for the help guys.

  • 1
    Have you got any error, or you got wrong output? please explain what you got and what you want? – Nilesh Apr 19 '16 at 18:19
  • 1
    Can you please update same in question? Also give full traceback. – Nilesh Apr 19 '16 at 18:25
  • Sorry, full traceback? – Kingbuttmunch Apr 19 '16 at 18:30
  • Full tracebak means, what you get on console. – Nilesh Apr 19 '16 at 18:31
  • 1
    There are many mistakes :) `datetime` has no constructor for `days` as first argument. Your are passing `year` in days and thats why it gives error. Check `datetime.datetime` doc. https://docs.python.org/2/library/datetime.html#datetime.datetime – Nilesh Apr 19 '16 at 18:34
  • 1
    You have to correct your code in `datetime(datetime.day,datetime,month,datetime.year,23,00)` – Nilesh Apr 19 '16 at 18:44
  • related: [Find if 24 hrs have passed between datetimes - Python](http://stackoverflow.com/q/26313520/4279) – jfs Apr 21 '16 at 17:35

1 Answers1

4

The following would demonstrate how to do what you want to achieve

from datetime import datetime


today = datetime.today()
y = today.year
m = today.month
d = today.day

late = datetime(y,m,d,23,0,0)

now = datetime.now()

timeDiff = late - now
print 'So if the time is %s:%s, That means you have %s minutes left' % (now.hour,now.minute,timeDiff.seconds/60) 

The __sub__ method has been implemented for datetime but obviously to use the operator both operands have to be of type datetime.

>>> datetime.now().__class__
<type 'datetime.datetime'>

When you subtract one datetime object from another you get an object of type timedelta.

>>> (datetime(2016,04,19)-datetime(2016,04,18)).__class__
>>> <type 'datetime.timedelta'>

datetime represents a timestamp while timedelta represents time difference.

It is possible to subtract a timedelta object from a datetime object but not vice versa and that is self-explanatory.

>>> datetime(2016,10,1)-timedelta(1)
datetime.datetime(2016, 9, 30, 0, 0)
Spade
  • 2,220
  • 1
  • 19
  • 29
  • I'm so grateful to see this answer because `datetime` works fine for complicated things but the fact it won't left you subtract `.time()` objects has always made frustrated me as it seems the logical thing to do for these kind of tasks. There's so many irregularities (no `strptime` for time objects, but they give you `strftime`) it feels unwieldy. Upvote :) – roganjosh Apr 19 '16 at 19:00
  • @roganjosh If you want to subtract a time object from a datetime object convert your time object into datetime. You can do that using [this simple recipe](http://stackoverflow.com/a/1697907/1309045) – Spade Apr 19 '16 at 19:17
  • Thanks, but it's not that I'm unable to do it, it's just that the logical process for me would be `now = datetime.datetime.now().time()` and `deadline = datetime.datetime.strptime("23:00:00", "%H:%M:%S").time()`. I understand that time for sure needs a date context for robust systems, but for day-to-day analysis for a non-programmer, the `datetime` module seems to go for maximum complexity as standard, rather than minimalism and ability to add complexity as needed. This is different than any other Python module I have encountered :) – roganjosh Apr 19 '16 at 19:23
  • I agree that datetime can be counter-intuitive. But the only thing that stops you from doing a direct subtraction is that you are explicitly not specifying the day which is an essential component of a timestamp. In fact `datetime.strptime("2016-04-19 23:00:00", "%Y-%m-%d %H:%M:%S") - datetime.now()` works just fine. – Spade Apr 19 '16 at 19:45
  • My task was, each day, to look at time stamps of when something was delivered and check whether it was within the time slot requested by a customer. It is always day-independent (even with daylight saving). That is very counter-intuitive currently. As Data Analysts/Scientists flock to Python, I think these simple features will be added because it's excessive atm. MS Excel has the lead for this task, which is sad :( – roganjosh Apr 19 '16 at 19:52
  • beware, `datetime(y,m,d,23) - datetime.now()` may fail if the utc offset for the local time zone is not the same for the both time instances e.g., if `now` and `late` are across DST boundaries. – jfs Apr 21 '16 at 17:34
  • Sure, this can go wrong if the clock is set incorrectly on a machine that runs either of now or late. The answer provided applies when ***other conditions remain constant or predictable*** – Spade Apr 21 '16 at 17:50