By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,035 Members | 1,543 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,035 IT Pros & Developers. It's quick & easy.

how do you manipulate the days on a calender

P: 8
when I did this on hypercard all i had to do was:

put the date into date
convert date to secs(onds)
date =+ (24*60*60*No_of_days_i_want_to_add_on)
convert date to the date

Is there any way I could do that with Python
Jan 8 '14 #1
Share this Question
Share on Google+
4 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
Here's an example of using built-in modules datetime and time to parse a date string and add days:
Expand|Select|Wrap|Line Numbers
  1. import datetime as dt
  2. import time
  3.  
  4. userDate = "2007/08/20"
  5. timeObj = time.strptime(userDate, "%Y/%m/%d")
  6. daysToAdd = 15
  7. dateObj = dt.datetime(*timeObj[:6])
  8.  
  9. print dateObj
  10. print dateObj + dt.timedelta(days=daysToAdd)
Jan 8 '14 #2

P: 8
this works great thank you, you got me out of a bind again, I've looked it over and messed with it to see how it works, but I can't figer out why the
Expand|Select|Wrap|Line Numbers
  1. [:6]
as in
Expand|Select|Wrap|Line Numbers
  1. dateObj = dt.datetime(*timeObj[:6])
also what is dt as in
Expand|Select|Wrap|Line Numbers
  1. import datetime as dt
Jan 9 '14 #3

bvdet
Expert Mod 2.5K+
P: 2,851
The time object has additional attributes that would not be accepted by the datetime.datetime constructor. That's why I take a slice of the time object ([:6]) which returns the first 6 elements. It works because a time object is iterable.

"import datetime as dt" is a way to abbreviate datetime.
Expand|Select|Wrap|Line Numbers
  1. >>> for attr in timeObj:
  2. ...     print attr
  3. ...     
  4. 2007
  5. 8
  6. 20
  7.  
  8.  
  9.  
  10.  
  11. 232
  12. -1
  13. >>> timeObj[:6]
  14. (2007, 8, 20, 0, 0, 0)
  15. >>> 
Jan 9 '14 #4

P: 8
very cool

thanks again
Jan 9 '14 #5

Post your reply

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