python - How to deduct the current time from a set time -


so trying create 'planner' or diary. trying create code advises how time have left in day before go bed or before have activity have planned.

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

from looking @ other threads approach have tried several other methods like

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

again saw on other threads. have tried many methods have begun confuse myself how solve it.

thanks again in advance

edit: when using

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

i got valuerror 'day out of range month'

and current code

late = timedelta(23,00)  

i got typeerror 'unsupported operand type(s) -: 'datetime.timedelta' , 'tuple' in example.

i want code output time difference between 11:00pm (or 23:00) , current time in minutes. sorry not being clearer

answered edit: thank comments , answer problem making code complicated , not using datetime() correctly,

    y = today.year     m = today.month     d = today.day      late = datetime(y,m,d,23,0,0) 

i had year month , day wrong way round , had format time wrong.

thanks guys.

the following demonstrate how want 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)  = datetime.now()  timediff = late - print 'so if time %s:%s, means have %s minutes left' % (now.hour,now.minute,timediff.seconds/60)  

the __sub__ method has been implemented datetime use operator both operands have of type datetime.

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

when subtract 1 datetime object object of type timedelta.

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

datetime represents timestamp while timedelta represents time difference.

it possible subtract timedelta object datetime object not vice versa , self-explanatory.

>>> datetime(2016,10,1)-timedelta(1) datetime.datetime(2016, 9, 30, 0, 0) 

Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -