I tried making a program that tells me the time before a specifide date, but it's really hard! I tried one of the ways that a person sent me, but it doesn't work...
How do you find the days before a date with time() (RE-VISIT)
Collapse
X
-
You really need to read the posting guidelines (particularly 'How to Ask a Question'), as you consistently violate them.
What "doesn't work"? What did you try? You need to provide some semblance of information if you expect us to help you. -
Hey try this if it suits your requirement
This will compute the number of days that has passed if the number of seconds or the date in dd/mm/yyyy format is given
Code:import time import string ,sys class findtime: def __init__(self): self.secperday=86400 self.uinput=raw_input("Enter the number of seconds or the format dd/mm/yyyy: ") def checktime(self): """ Computations with epoch time working with dates after 1970 """ self.presentsec = time.time() self.pform= time.localtime() if '/' in self.uinput : frdate=string.split(self.uinput,'/') consdate = tuple([int(frdate[2]),int(frdate[1]),int(frdate[0]),self.pform[3],self.pform[4],self.pform[5],self.pform[6],self.pform[7],self.pform[8]]) diff = self.presentsec - (time.mktime(consdate)) print "Number of seconds Passed %d" %diff print "Number of days passed %d" % (diff/self.secperday) else : self.uinput =float(self.uinput) print ("Computation for %s seconds from present date" %self.uinput) self.expectedsec = self.presentsec - self.uinput self.expectedday=time.localtime(self.expectedsec) print ("Expected %d/%d/%d"%(self.expectedday[2],self.expectedday[1],self.expectedday[0])) diffdays = int(round(self.uinput / self.secperday)) print "Number of days passed %d" % (diffdays) if __name__=='__main__': obj = findtime() obj.checktime()Comment
-
It seems like I posted this before. It uses time and datetime.
[code=Python]import datetime, time
def convert_seconds (seconds):
hours = int(seconds/3600)
mins = int((seconds-hours*3600)/60)
secs = seconds-hours*3600-mins*60
return hours, mins, secs
present = datetime.dateti me.now()
print '''Calculate the difference between the present time and another
date, either past or future (present - other date).'''
dateStr = raw_input('Ente r the date (mm/dd/yyyy)')
timeObj = time.strptime(d ateStr, '%m/%d/%Y')
datetimeObj = datetime.dateti me(*timeObj[:6])
delta = present-datetimeObj
hours, mins, secs = convert_seconds (delta.seconds)
print 'The difference is %d day%s, %d hour%s, %d minute%s, and %d second%s.' % \
(delta.days, ['s', ''][abs(delta.days) ==1 or 0],\
hours, ['s', ''][abs(hours)==1 or 0],\
mins, ['s', ''][abs(mins)==1 or 0],\
secs, ['s', ''][abs(secs)==1 or 0])[/code]
[code=Python]>>> Calculate the difference between the present time and another
date, either past or future (present - other date).
The difference is -29 days, 8 hours, 35 minutes, and 19 seconds.
[/code]Comment
Comment