473,327 Members | 1,892 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,327 software developers and data experts.

Date Addition

56
Hi,

how can I add calendar days in which it will exclude sundays?

ex: date today is MAY 1 then i will add 6 days, so it will return MAY 8 since sunday was excluded..

thanks guys....
May 19 '08 #1
3 1335
jlm699
314 100+
This is crude and done quick and dirty but here's an idea to get you started:
Expand|Select|Wrap|Line Numbers
  1. >>> import datetime
  2. >>> dt = datetime.date(2008, 5, 1)
  3. >>> delta = 6
  4. >>> if dt.weekday() > 7 - delta:
  5. ...     delta += ( ( delta / 7 ) + [0, 1][bool(delta % 7)] )
  6. ...     
  7. >>> dt + datetime.timedelta(delta)
  8. datetime.date(2008, 5, 8)
  9. >>> delta = 20
  10. >>> if dt.weekday() > 7 - delta:
  11. ...     delta += ( ( delta / 7 ) + [0, 1][bool(delta % 7)] )
  12. ...     
  13. >>> dt + datetime.timedelta(delta)
  14. datetime.date(2008, 5, 24)
  15. >>> 
That's not perfect but it's very close... only a minor modification to fix
May 20 '08 #2
heiro
56
This is crude and done quick and dirty but here's an idea to get you started:
Expand|Select|Wrap|Line Numbers
  1. >>> import datetime
  2. >>> dt = datetime.date(2008, 5, 1)
  3. >>> delta = 6
  4. >>> if dt.weekday() > 7 - delta:
  5. ...     delta += ( ( delta / 7 ) + [0, 1][bool(delta % 7)] )
  6. ...     
  7. >>> dt + datetime.timedelta(delta)
  8. datetime.date(2008, 5, 8)
  9. >>> delta = 20
  10. >>> if dt.weekday() > 7 - delta:
  11. ...     delta += ( ( delta / 7 ) + [0, 1][bool(delta % 7)] )
  12. ...     
  13. >>> dt + datetime.timedelta(delta)
  14. datetime.date(2008, 5, 24)
  15. >>> 
That's not perfect but it's very close... only a minor modification to fix


yeah much thanks for this.. i will do some modification..
May 20 '08 #3
Hey try this one !! hope this suits your requirement
I had a workaround with the time module

Expand|Select|Wrap|Line Numbers
  1. import time
  2. import string ,sys
  3.  
  4. class workalday:
  5.  
  6.     def __init__(self):
  7.  
  8.         self.secperday=86400
  9.  
  10.     def addtime(self):
  11.         """ Computations with epoch time working with dates after 1970"""
  12.  
  13.         print "Provide with date in format dd/mm/yyyy to compute Number of sundays till then "
  14.         sys.stderr.write("Else provide with number of days for computation: ")
  15.         self.uinput=raw_input()
  16.         if '/' in self.uinput :
  17.             self.fomats(1)
  18.         else :
  19.             print ("Computation for %s days from present date" %self.uinput)
  20.             self.fomats(2)
  21.             print ("Expected %d/%d/%d"%(self.lastdate,self.lastmonth,self.lastyear))
  22.  
  23.     def deadday(self,dd):
  24.         """ Computation of sundays"""
  25.         updatetime = 0
  26.         sdays = int(dd)
  27.         for i in range(sdays):
  28.             updatetime = updatetime + self.secperday
  29.             if time.localtime(self.presentsec + updatetime)[6] == 6:
  30.                 sdays = sdays + 1
  31.  
  32.         return sdays
  33.  
  34.     def fomats(self,state):
  35.  
  36.         """ Compute the sec and formated dates from actual to specified"""
  37.         self.presentsec = time.time()
  38.         self.pform= time.localtime()
  39.         if state == 2 :
  40.             """ If number of days specified"""
  41.             sunday =  self.deadday(self.uinput)
  42.             self.display(sunday)
  43.  
  44.         if state == 1 :
  45.             """ If date computations specified to date"""
  46.             frdate=string.split(self.uinput,'/')
  47.             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]])
  48.             finsec = time.localtime(time.mktime(consdate))
  49.             diffdays =  int(round((time.mktime(consdate) - self.presentsec ) / self.secperday))
  50.             sunday =  self.deadday(diffdays)
  51.             print ("The Number of sundays that will be excuded: %d" %sunday)
  52.  
  53.     def display(self,sdays):
  54.  
  55.         self.lastyear = time.localtime(self.presentsec + (sdays*self.secperday))[0]
  56.         self.lastmonth  = time.localtime(self.presentsec + (sdays*self.secperday))[1]
  57.         self.lastdate = time.localtime(self.presentsec + (sdays*self.secperday))[2]
  58.  
  59. if __name__=='__main__':
  60.  
  61.     obj = workalday()
  62.     obj.addtime()
May 21 '08 #4

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

Similar topics

2
by: androtech | last post by:
Hello, I'm looking for a function that returns a date range for a specified week number of the year. I'm not able to find functions like this anywhere. Any pointers/help would be much...
20
by: rhino | last post by:
This worked before October now it does not work. I think I know where the trouble is but I don't know how to fix it. function findVerse() { var d = new Date(); var str; if( (...
13
by: Deano | last post by:
Hi, I generate a report using two dates (From and To). I notice if I enter 01/10/2003 that it is interpreted by Access as 10/01/2003 i.e 10th January rather than 1st October as I intended. ...
12
by: Steve Elliott | last post by:
I have a query set up to gather together data between two specified dates. Shown in the query column as: Between #24/09/2004# And #01/10/2004# Is it possible to enter several different date...
15
by: Khurram | last post by:
I have a problem while inserting time value in the datetime Field. I want to Insert only time value in this format (08:15:39) into the SQL Date time Field. I tried to many ways, I can extract...
6
by: rohayre | last post by:
Im a long time java developer and actually have never done anything with java scripting. I'd like to write a short simple script for calculating a date in the future based on today's date and a...
6
by: Luvin lunch | last post by:
Hi, I'm new to access and am very wary of dates as I have limited experience in their manipulation and I know if they're not done properly things can turn ugly quickly. I would like to use a...
44
by: user | last post by:
Hi, Let's say I have 2 dates in the b/m format: Date 1 and date 2 How do I check whether Date2 is later than Date 1? Date1. 21-Nov-2006 09:00:00 PM
2
by: sixdeuce62 | last post by:
Hello, I am trying to create a query that will prompt me to enter the parameter value if beginning date and ending date. I have created everything I need in the query, but I have to manually go...
7
by: kr151080 | last post by:
Ok so I am messing around with a program and have no idea how to go about doing this but here is the code for the class date.... public class Date { private int dMonth; private int dDay;...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.