Calendar Arithmetic (Python)

Please support our Python advertiser: Programming Forums
Mar 27th, 2005
Views: 21,804
Thread Rating: 1 votes, 4.0000 average.
AddThis Social Bookmark Button
The module datetime allows you to call several functions that can do computation with dates and times. How many days since my birth? How many days till Xmas? What's the date 77 days from now? The code snippet should give you a flavor, enough to explore on your own.
python Syntax
  1. # testing (or tasting) the module datetime and its method date
  2. # datetime supports calendar arithmetic
  3. # strftime() requires the year to be >=1900
  4. # date() can handle years down to 0001
  5. # Python24 tested vegaseat 3/27/05
  6.  
  7. import datetime
  8. import calendar
  9.  
  10. today = datetime.date.today()
  11. oneday = datetime.timedelta(days=1)
  12. # loop back to most recent Friday (could be today)
  13. # takes care of end of month and even leap years
  14. friday = today
  15. while friday.weekday() != calendar.FRIDAY:
  16. friday -= oneday
  17. oneweek = datetime.timedelta(days=7)
  18. nextweek = today + oneweek
  19. nextyear = today.replace(year=today.year+1)
  20. print "Today (year-month-day) =", today
  21. print "Most recent Friday =", friday
  22. print "One week from today =", nextweek
  23. print "One year from today =", nextyear
  24. print "Current time =", datetime.datetime.now().time() #just the time
  25. print "Lunchtime =", datetime.time(12, 00) # 12:00:00
  26.  
  27. print '-'*50
  28.  
  29. # a typical birthday year, month, day
  30. # or change it to your own birthday...
  31. birthday = datetime.date(1955, 4, 1)
  32. # three different ways to present a date
  33. # 1955-04-01
  34. print "Birthday format1:", birthday
  35. # 04/01/55
  36. print "Birthday format2:", birthday.strftime("%m/%d/%y")
  37. # 01Apr1955
  38. print "Birthday format3:", birthday.strftime("%d%b%Y")
  39.  
  40. age = today - birthday
  41. print "You are", age.days, "days old today!"
  42.  
  43. # extract the year from todays date
  44. thisyear = int(today.strftime("%Y")[0:4])
  45.  
  46. # party message (include date and time) ...
  47. rightnow = datetime.datetime.today()
  48. bparty = datetime.datetime(thisyear, 4, 1, 14, 30)
  49. # check if you can still make it to the party ...
  50. if bparty > rightnow:
  51. print "Birthday party", bparty
  52. else:
  53. print "Missed this year's birthday party!"
  54.  
  55. print '-'*50
  56.  
  57. # calculate days till xmas of this year
  58. xmas = datetime.date(thisyear, 12, 25)
  59. tillXmas = xmas - today
  60. print "There are %d days till xmas!" % tillXmas.days
  61.  
  62. print '-'*50
  63.  
  64. # add days to a given date
  65. delta = datetime.timedelta(days=77)
  66. addDays = today + delta
  67. print "Today's date is :", today.strftime("%d%b%Y")
  68. print "77 days from today it will be :", addDays.strftime("%d%b%Y")
  69.  
  70. print '-'*50
  71.  
  72. Weekday = ['Monday', 'Tuesday', 'Wednesday', 'Thursday',
  73. 'Friday', 'Saturday', 'Sunday']
  74. print "Today is", Weekday[datetime.date.weekday(today)]
  75. print
  76. print "If you were born on %s you were born on a %s" % (birthday.strftime("%d%b%Y"), Weekday[datetime.date.weekday(birthday)])

Only community members can submit or comment on code snippets. You must register or log in to contribute.

Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 10:55 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC