Posted by Anonymous on Mon 5 May 09:26
report abuse | download | new post
- #!/usr/bin/env python
- #coding=utf-8
- #daysDelta24.py
- """
- Compute the difference in days (and weeks and days) between 2 dates.
- E.g., The difference between 07/04/1776 and 09/11/2001 is 82248 days
- Or 11749 weeks and 5 days
- """
- from mx import DateTime as DT
- import time
- import calendar
- def getDate():
- while True:
- try:
- dstr = raw_input("Enter date as month/day/year, or enter nothing for today: ")
- if dstr == "":
- return DT.now()
- else:
- return DT.Parser.DateFromString(dstr, ('us',))
- except ValueError:
- print "\nDate was in wrong form."
- print "Enter date in form m/d/yyyy, e.g. 07/04/1776, 1/18/2009, etc."
- def DayOfWeek(year, month, day):
- from calendar import weekday
- dayNumber = calendar.weekday(year, month, day)
- dayNames = ('Monday', 'Tuesday', 'Wednesday', 'Thursday',
- 'Friday', 'Saturday', 'Sunday')
- return dayNames[dayNumber]
- def printDate(date):
- date2 = date.strftime("%Y,%m,%d")
- year, month, day = date2[0:4], date2[5:7], date2[8:10]
- astr = "Date entered was %s, a %s"
- print astr % (date.strftime("%m/%d/%Y"), DayOfWeek(int(year), int(month), int(day)))
- def printInstruction(flag):
- if flag == 'date1':
- print "Enter 2 dates."
- print "What's one of the dates?"
- else:
- print "What's the other date?"
- def calculateDaysAndWeeksDiffs(date1, date2):
- days1 = int(abs(date2 - date1).days)
- weeks = days1//7
- days2 = days1 % 7
- return days1, weeks, days2
- def printResult(date1, date2, days1, weeks, days2):
- print "\nThe difference between %s and %s is %d days" % (date1.strftime("%m/%d/%Y"),
- date2.strftime("%m/%d/%Y"), days1)
- print "Or %d weeks and %d days" % (weeks, days2)
- print "=================================================================="
- def main():
- printInstruction(flag='date1')
- date1 = getDate()
- printDate(date1)
- printInstruction(flag='date2')
- date2 = getDate()
- printDate(date2)
- days1, weeks, days2 = calculateDaysAndWeeksDiffs(date1, date2)
- printResult(date1, date2, days1, weeks, days2)
- if __name__ == '__main__':
- while True:
- main()
- print
- ans = raw_input("Again? Press Enter for yes; Enter 'n' for no: ")
- if ans == 'n':
- print
- print "Bye"
- time.sleep(1.5)
- break
- print
- """
- Outputs:
- ==============#1================================
- Enter 2 dates.
- What's one of the dates?
- Enter date as month/day/year, or enter nothing for today: 5/5/2008
- Date entered was 05/05/2008, a Monday
- What's the other date?
- Enter date as month/day/year, or enter nothing for today: 5/8/2008
- Date entered was 05/08/2008, a Thursday
- The difference between 05/05/2008 and 05/08/2008 is 3 days
- Or 0 weeks and 3 days
- ===============#2===============================
- Enter 2 dates.
- What's one of the dates?
- Enter date as month/day/year, or enter nothing for today: 5/8/2008
- Date entered was 05/08/2008, a Thursday
- What's the other date?
- Enter date as month/day/year, or enter nothing for today: 5/5/2008
- Date entered was 05/05/2008, a Monday
- The difference between 05/08/2008 and 05/05/2008 is 3 days
- Or 0 weeks and 3 days
- ================#3===============================
- Enter 2 dates.
- What's one of the dates?
- Enter date as month/day/year, or enter nothing for today:
- Date entered was 05/05/2008, a Monday
- What's the other date?
- Enter date as month/day/year, or enter nothing for today: 5/8/2008
- Date entered was 05/08/2008, a Thursday
- The difference between 05/05/2008 and 05/08/2008 is 2 days
- Or 0 weeks and 2 days
- =================#4================================
- What's one of the dates?
- Enter date as month/day/year, or enter nothing for today: 5/8/2008
- Date entered was 05/08/2008, a Thursday
- What's the other date?
- Enter date as month/day/year, or enter nothing for today:
- Date entered was 05/05/2008, a Monday
- The difference between 05/08/2008 and 05/05/2008 is 2 days
- Or 0 weeks and 2 days
- ==================end==============================
- """
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.