Advertisement
Guest User

Calculate Difference Between Dates

a guest
Apr 26th, 2015
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.38 KB | None | 0 0
  1. # A script for calculating the difference between two dates in years, months, and days.
  2.  
  3.  
  4. # Function definitions:
  5.  
  6.  
  7. # Function to ask user for dates and assign them to variables.
  8.  
  9. def input_dates():
  10.  
  11.     print "What is the start date?"
  12.    
  13.     # gets user input for start date in 3 separate variables
  14.    
  15.     start_month = raw_input("Month: ")
  16.     start_day = raw_input("Day: ")
  17.     start_year = raw_input("Year: ")
  18.    
  19.    
  20.     print "\nThat's a dope ass date. Now what's the end date?"
  21.    
  22.     # gets user input for end date in 3 separate variables
  23.    
  24.     end_month = raw_input("Month: ")
  25.     end_day = raw_input("Day: ")
  26.     end_year = raw_input("Year: ")
  27.    
  28.    
  29.     start_date = [start_month, start_day, start_year] # stores start date variables in a single list
  30.     end_date = [end_month, end_day, end_year] # stores end date variables in a single list
  31.    
  32.    
  33.     return (start_date, end_date) # returns start date and end date as lists
  34.  
  35.  
  36. # Function to find the difference between two dates
  37.  
  38. def date_difference(date1, date2):
  39.  
  40.  
  41. # assign variables to start year, end year, and their difference
  42.  
  43.     year1 = int(date1[2])
  44.     year2 = int(date2[2])
  45.     year_diff = year2 - year1
  46.    
  47. # assign variables for days    
  48.  
  49.     day1 = int(date1[1])
  50.     day2 = int(date2[1])
  51.    
  52.    
  53. # assign a list variable containing all months to reference via index
  54.    
  55.     months = ["January", "February", "March", "April", "May", "June", "July",
  56.             "August", "September", "October", "November", "December"]
  57.  
  58.  
  59. # assign string variable to user month inputs
  60.  
  61.     month1 = date1[0] # defines start month as first string in start date list
  62.     month2 = date2[0] # defines end month as first string in end date list
  63.    
  64.    
  65. # iterates through months list until matching month is found for start month
  66.    
  67.     for month in months:
  68.         if month != month1:
  69.             continue
  70.         month_num1 = int(months.index(month)) # assigns integer value to month after match is found
  71.        
  72.  
  73. # iterates through months list until matching month is found for end month
  74.        
  75.     for month in months:
  76.         if month != month2:
  77.             continue
  78.         month_num2 = int(months.index(month)) # assigns integer value to month after match is found
  79.    
  80.     # print month_num1 # check loop
  81.     # print month_num2 # check loop
  82.    
  83. # finds month difference.
  84.  
  85.     if month_num2 < month_num1: # accounts for case when end month precedes start month in end year
  86.         month_diff = 12 - (month_num1 - month_num2)
  87.         year_diff -= 1 # subtracts one year from total if end month precedes start month in end year
  88.     elif month_num2 > month_num1:
  89.          month_diff = month_num2 - month_num1
  90.     else: # special cases for when the start month and end month are the same
  91.         if day2 >= day1:
  92.             month_diff = 0
  93.         else:
  94.             month_diff = 11
  95.             year_diff -= 1
  96.        
  97.          
  98.    
  99. # assigns a value for the number of days in the month prior to the end month. necessary to calculate day difference
  100.  
  101.     if month_num2 in (0,1,3,5,7,8,10):
  102.         month_len = 31
  103.     elif month_num2 in (4,6,9,11):
  104.         month_len = 30
  105.     else: # checks if leap year for February days
  106.         if (year2 % 4 == 0 and year2 % 100 != 0) or year2 % 400 == 0: # this is the necessary condition for a leap year to exist
  107.             month_len = 29
  108.         else:
  109.             month_len = 28
  110.            
  111.            
  112.            
  113. # finds day difference. accounts for preceding day numbers in later months e.g. june 20 to march 2
  114.  
  115.     if day2 < day1:
  116.         day_diff = month_len + day2 - day1
  117.     else:
  118.         day_diff = day2 - day1
  119.        
  120.  
  121. # prints difference in years, months, and days.
  122.  
  123.     print "\nWow. Thats %d years, %d months, and %d days!\n" % (year_diff, month_diff, day_diff)
  124.    
  125. # returns values to be used for total days calculation
  126.  
  127.     return (year_diff, day_diff, year1, year2, day1, day2, month_num1, month_num2, months)
  128.  
  129.    
  130. # used to check calculations and variable values for debugging
  131.  
  132. """ # check variable values
  133.     print month_num1
  134.     print month_num2
  135.     print month_len
  136.     print year_diff
  137.     print day1
  138.     print day2
  139.     print month_diff
  140.     print day_diff """
  141.    
  142.  
  143.  
  144. # Function to calculate total number of days. Will take all return values from date_difference function
  145.    
  146. def total_day_count(years, days, year1, year2, day1, day2, month1, month2, months):
  147.  
  148.  
  149.     year_days = years * 365 # calculates number of days contained in full years
  150.        
  151.  
  152. # finds number of days in month prior to end month, adds remaining number of days from start date to total days
  153.  
  154.        
  155.     if month2 in (0,1,3,5,7,8,10):
  156.         month_end_len = 31
  157.     elif month2 in (4,6,9,11):
  158.         month_end_len = 30
  159.     else: # checks if leap year for February days
  160.         if (year2 % 4 == 0 and year2 % 100 != 0) or year2 % 400 == 0:
  161.             month_end_len = 29
  162.         else:
  163.             month_end_len = 28
  164.        
  165.        
  166.     # print month2 # check variable
  167.     # print month_end_len # check variable
  168.    
  169.    
  170.     total_days = year_days + (month_end_len - day1) + day2 # starts day total with complete year days plus days in start and end months
  171.    
  172.     # print total_days # check calculation
  173.        
  174.        
  175.        
  176. # the next two loops calculate the number of days in all the months between start and end month. adds days to total at every loop iteration
  177.  
  178.  
  179. # first case: when the end month precedes start month in end year e.g. june 2014 to april 2015
  180.  
  181.     if month2 < month1:
  182.         for month in range(month1+1, 12): # loops over all months between start month and end of year, excluding start month
  183.             if month in (0,2,4,6,7,9,11):
  184.                 month_len = 31
  185.                 total_days += month_len
  186.             elif month in (3,5,8,10):
  187.                 month_len = 30
  188.                 total_days += month_len
  189.             else: # if the month is February, it checks for a leap year then assigns the month length
  190.                 if (year1 % 4 == 0 and year1 % 100 != 0) or year1 % 400 == 0:
  191.                     month_len = 29
  192.                     total_days += month_len
  193.                 else:
  194.                     month_len = 28
  195.                     total_days += month_len
  196.                    
  197.         for month in range(0, month2): # loops over all month between beginning of year and end month, excluding end month
  198.             if month in (0,2,4,6,7,9,11):
  199.                 month_len = 31
  200.                 total_days += month_len
  201.             elif month in (3,5,8,10):
  202.                 month_len = 30
  203.                 total_days += month_len
  204.             else: # same February calculation as above
  205.                 if (year1 % 4 == 0 and year1 % 100 != 0) or year1 % 400 == 0:
  206.                     month_len = 29
  207.                     total_days += month_len
  208.                 else:
  209.                     month_len = 28
  210.                     total_days += month_len
  211.                    
  212.                    
  213. # second case: when end month is after start month in later year e.g. june 2014 to august 2015
  214.  
  215.     if month2 > month1: # loops over all months between start month and end month. excluding start and end months
  216.         for month in range(month1+1, month2):
  217.             if month in (0,2,4,6,7,9,11):
  218.                 month_len = 31
  219.                 total_days += month_len
  220.             elif month in (3,5,8,10):
  221.                 month_len = 30
  222.                 total_days += month_len
  223.             else:
  224.                 if (year1 % 4 == 0 and year1 % 100 != 0) or year1 % 400 == 0:
  225.                     month_len = 29
  226.                     total_days += month_len
  227.                 else:
  228.                     month_len = 28
  229.                     total_days += month_len
  230.                    
  231. # third case: when end month and start month are the same.
  232.  
  233.     if month2 == month1 and years == 0:
  234.         total_days = 365 + (day2 - day1)
  235.        
  236.     if month2 == month1 and years != 0:
  237.         total_days = (years * 365) + (365 + (day2 - day1))
  238.            
  239.  
  240.  
  241. # adds days for leap years that are present in the year span.
  242.  
  243.     for year in range(year1, year2): # the end year isn't included because February would already be accounted for in the month days calculation above
  244.         if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
  245.             total_days += 1
  246.             # print year # check loop
  247.            
  248.              
  249.     print "Damn son. That's %d days!\n" % total_days # prints total days
  250.    
  251.    
  252.     return (total_days) # returns total days for conversion
  253.    
  254.    
  255. # Function to convert total days to other units of time and print them
  256.  
  257. def convert(days):
  258.    
  259.     hours = days * 24
  260.     minutes = hours * 60
  261.     seconds = minutes * 60
  262.     weeks = days / 7
  263.     extra_days = days % 7
  264.     hexsecs = float(seconds * 1.32)
  265.    
  266.     print "If you wanna get wild, that's equal to:\n"
  267.     print "%d hours" % hours
  268.     print "%d minutes" % minutes
  269.     print "%d seconds" % seconds
  270.     print "%d weeks and %d days" % (weeks, extra_days)
  271.     print "And %d hexadecimal seconds! (wtf is that anyway?)\n" % hexsecs
  272.            
  273.            
  274. # end function definitions
  275.    
  276.  
  277.  
  278. # start script
  279.  
  280.  
  281. print "\nOh shit. You have to go to China on a secret mission?! How long are you gonna be there?\n"
  282.  
  283.  
  284. # calls functions and assigns variables to return values
  285.  
  286. (start, end) = input_dates()
  287.  
  288. (years, days, year1, year2, day1, day2, month1, month2, months) = date_difference(start, end)
  289.  
  290. total_days = total_day_count(years, days, year1, year2, day1, day2, month1, month2, months)
  291.  
  292. convert(total_days)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement