473,396 Members | 1,814 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How do you find the days before a date with time() (RE-VISIT)

raubana
56
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...
May 22 '08 #1
5 1430
jlm699
314 100+
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.
May 22 '08 #2
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

Expand|Select|Wrap|Line Numbers
  1. import time
  2. import string ,sys
  3.  
  4. class findtime:
  5.  
  6.     def __init__(self):
  7.  
  8.         self.secperday=86400
  9.         self.uinput=raw_input("Enter the number of seconds or the format dd/mm/yyyy: ")
  10.  
  11.     def checktime(self):
  12.         """
  13.         Computations with epoch time working with dates after 1970
  14.         """
  15.         self.presentsec = time.time()
  16.         self.pform= time.localtime()
  17.  
  18.         if '/' in self.uinput :
  19.             frdate=string.split(self.uinput,'/')
  20.             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]])
  21.             diff =   self.presentsec - (time.mktime(consdate))
  22.             print "Number of seconds Passed %d" %diff
  23.             print "Number of days passed %d"  % (diff/self.secperday)
  24.  
  25.         else :
  26.             self.uinput =float(self.uinput)
  27.             print ("Computation for %s seconds from present date" %self.uinput)
  28.             self.expectedsec = self.presentsec - self.uinput
  29.             self.expectedday=time.localtime(self.expectedsec)
  30.             print ("Expected %d/%d/%d"%(self.expectedday[2],self.expectedday[1],self.expectedday[0]))
  31.             diffdays =  int(round(self.uinput / self.secperday))
  32.             print "Number of days passed %d"  % (diffdays)
  33.  
  34. if __name__=='__main__':
  35.  
  36.     obj = findtime()
  37.     obj.checktime()
May 23 '08 #3
bvdet
2,851 Expert Mod 2GB
It seems like I posted this before. It uses time and datetime.
Expand|Select|Wrap|Line Numbers
  1. import datetime, time
  2.  
  3. def convert_seconds(seconds):
  4.     hours = int(seconds/3600)
  5.     mins = int((seconds-hours*3600)/60)
  6.     secs = seconds-hours*3600-mins*60
  7.     return hours, mins, secs
  8.  
  9. present = datetime.datetime.now()
  10.  
  11. print '''Calculate the difference between the present time and another
  12. date, either past or future (present - other date).'''
  13.  
  14. dateStr = raw_input('Enter the date (mm/dd/yyyy)')
  15. timeObj = time.strptime(dateStr, '%m/%d/%Y')
  16. datetimeObj = datetime.datetime(*timeObj[:6])
  17. delta = present-datetimeObj
  18. hours, mins, secs = convert_seconds(delta.seconds)
  19.  
  20. print 'The difference is %d day%s, %d hour%s, %d minute%s, and %d second%s.' % \
  21.       (delta.days, ['s', ''][abs(delta.days)==1 or 0],\
  22.        hours, ['s', ''][abs(hours)==1 or 0],\
  23.        mins, ['s', ''][abs(mins)==1 or 0],\
  24.        secs, ['s', ''][abs(secs)==1 or 0])
Expand|Select|Wrap|Line Numbers
  1. >>> Calculate the difference between the present time and another
  2. date, either past or future (present - other date).
  3. The difference is -29 days, 8 hours, 35 minutes, and 19 seconds.
  4.  
May 23 '08 #4
jlm699
314 100+
I tried one of the ways that a person sent me, but it doesn't work...
@bvdet: Yes you did provide him with this previously... He failed to elaborate why it didn't work however.
May 23 '08 #5
raubana
56
Yeah, I didn't explain why it didn't work before because I'm not sure why it didn't work... I do know, however, that the '('s, ')'s, '[' and the ']' may have been missing in some places.

Anyway, thanks for your help, guys! I'll try it out as soon as I can.
May 28 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Sugapablo | last post by:
I have a table that's of type date/time (i.e. 01/01/1900 00:00:00). What I want is to do the following: Say you have these records: person | date-time -------+---------------------------...
2
by: Andrew | last post by:
Hi, friends, I use the following C# code to find out date/time for all files in a directory: (Those files are retrieved from VSS). VSS_Item.Get(ref tempFile, (int)...
9
by: Shapper | last post by:
Hello, I created a dataset from a XML file. One of the dataset fields is . In an ASP:Repeater I am displaying the field. <%# DataBinder.Eval(Container.DataItem, "pubDate", "{0:f}") %> The...
2
by: Lad | last post by:
I use datetime class in my program and now I have two fields that have the datetime format like this datetime.datetime(2006, 5, 24, 16, 1, 26) How can I find out the date/time difference ( in...
5
by: Takeadoe | last post by:
Gang - I'm generating date and time variables from scanned forms. Currently, the date and time values are as follows: 06/26/2006 and 11:30 AM. I've written VBA code to combine them into a...
12
by: Killer42 | last post by:
Hi all. I have a 1GB Samsung MP3 player which is also a voice recorder and flash disk. Pretty standard stuff these days, I know. But what I want to know is, how can I set the date and time on it?...
1
by: geethaandyou | last post by:
I Need A C Program To Find The Date That Comes After N Days From Todays Date
1
by: rrayfield | last post by:
Any body have an Idea on this... Last problem of my Calendar Application. To keep my calendar loading fast and efficent I have a SP that loads data to a datatable to read from c#. My sp reads...
3
harshadd
by: harshadd | last post by:
I have a centralise domain, and people from diff regions in country keeps adding PC's and Users to domain. I do not have problem for they doing so, but my need is I want to know how many users/ Pcs...
4
by: lenygold via DBMonster.com | last post by:
I found this example in MYSQL: create table events ( id integer not null primary key , datetime_start datetime not null , datetime_end datetime not null ); insert into events values ( 1,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.