Comments on: Excel date arithmetic problem
http://ask.metafilter.com/106804/Excel-date-arithmetic-problem/
Comments on Ask MetaFilter post Excel date arithmetic problemFri, 14 Nov 2008 10:12:44 -0800Fri, 14 Nov 2008 10:12:44 -0800en-ushttp://blogs.law.harvard.edu/tech/rss60Question: Excel date arithmetic problem
http://ask.metafilter.com/106804/Excel-date-arithmetic-problem
MS Excel Question: If I have a column with a range of dates in it, is there a formula that will tell me how many years ago that date was from today? <br /><br /> I am trying to calculate employee discount levels. For every year of service, an employee gains an additional percentage discount. I have employee hire dates in an Excel spreadsheet, and I would like to be able to, at a glance, determine what discount level a given employee is at, even if I open the spreadsheet three years from now. The levels top out at five years, if that matters.post:ask.metafilter.com,2008:site.106804Fri, 14 Nov 2008 09:55:06 -0800Rock SteadyexcelmsexcelofficemsofficeformuladateyearresolvedBy: fuzzbean
http://ask.metafilter.com/106804/Excel-date-arithmetic-problem#1540941
=IF((TODAY()-A1)/365>=5,5,(TODAY()-A1))<br>
<br>
Where A1 is the column with the initial dates in it.comment:ask.metafilter.com,2008:site.106804-1540941Fri, 14 Nov 2008 10:12:44 -0800fuzzbeanBy: fuzzbean
http://ask.metafilter.com/106804/Excel-date-arithmetic-problem#1540943
Er, whoops. Left a bit out. <br>
<br>
=IF((TODAY()-A1)/365>=5,5,((TODAY()-A1)/365))comment:ask.metafilter.com,2008:site.106804-1540943Fri, 14 Nov 2008 10:13:49 -0800fuzzbeanBy: IAmBroom
http://ask.metafilter.com/106804/Excel-date-arithmetic-problem#1540946
=YEAR(NOW()-A1)-1900<br>
<br>
Again, cell A1 contains the desired past date. Format this output cell with a non-date number format (like "General" or "0").comment:ask.metafilter.com,2008:site.106804-1540946Fri, 14 Nov 2008 10:16:35 -0800IAmBroomBy: The Bellman
http://ask.metafilter.com/106804/Excel-date-arithmetic-problem#1540947
Sure, the "TODAY" function will give you today's date, and excell handles date subtraction as you would expect so the formula "=TODAY()-DATE(2008,11,13)" would return "1". Just do the subtraction and divide by 365. If you want to use 360 day years (12 30 day months) you can yse the DAYS360 function which will return the days between dates based on 360 day years.<br>
<br>
What format are the dates in? You can use the "DATEVALUE" function to convert them to excell format dates before subtracting -- oh and then just divide by 365 to get years, of course.comment:ask.metafilter.com,2008:site.106804-1540947Fri, 14 Nov 2008 10:17:14 -0800The BellmanBy: IAmBroom
http://ask.metafilter.com/106804/Excel-date-arithmetic-problem#1540948
Thanks for the update, <strong>fuzzbean</strong>. I was wondering what possessed you...comment:ask.metafilter.com,2008:site.106804-1540948Fri, 14 Nov 2008 10:17:15 -0800IAmBroomBy: The Bellman
http://ask.metafilter.com/106804/Excel-date-arithmetic-problem#1540949
Or, what fuzzbean said, as long as A1 is in excell date format.comment:ask.metafilter.com,2008:site.106804-1540949Fri, 14 Nov 2008 10:17:42 -0800The BellmanBy: Rock Steady
http://ask.metafilter.com/106804/Excel-date-arithmetic-problem#1540951
Hey, that's great fuzzbean. Can you explicate the formula a little for me? I'd like to understand why it works, if I can.comment:ask.metafilter.com,2008:site.106804-1540951Fri, 14 Nov 2008 10:19:40 -0800Rock SteadyBy: IAmBroom
http://ask.metafilter.com/106804/Excel-date-arithmetic-problem#1540959
Nothing personal against <strong>fuzzbean</strong> (since I already jibed him about his code error), but the formula I posted is shorter, clearer, and immune to leap year errors.<br>
<br>
If you want the 5-year max included, use:<br>
<br>
=MIN(YEAR(NOW()-A1)-1900, 5)comment:ask.metafilter.com,2008:site.106804-1540959Fri, 14 Nov 2008 10:26:19 -0800IAmBroomBy: Rock Steady
http://ask.metafilter.com/106804/Excel-date-arithmetic-problem#1540962
Thanks to you, too, IAmBroom.comment:ask.metafilter.com,2008:site.106804-1540962Fri, 14 Nov 2008 10:32:37 -0800Rock SteadyBy: fuzzbean
http://ask.metafilter.com/106804/Excel-date-arithmetic-problem#1540964
The Bellman's got it pretty much covered but here:<br>
<br>
=TODAY() returns today's date.<br>
TODAY()-A1 returns the number of days between today and the employee's start date.<br>
(TODAY()-A1)/365 converts that to years.<br>
<br>
Then it's nested in an IF function, which say basically, if condition A is met, return X, if condition A is not met, return Y.<br>
So in =IF(A1=4,1,0) A1=4 is condition A. A1=4 is our condition A; 1 is our X; 0 is our Y. If the value in A1 is 4, the formula will return 1. If the value is anything other than 4, it will return 0. <br>
<br>
=IF(<strong><em>(TODAY()-A1)/365>=5</em></strong>,5,((TODAY()-A1)/365)), the bolded italic part (TODAY()-A1)/365>=5 is condition A.<br>
=IF((TODAY()-A1)/365>=5,<em><strong>5</strong></em>,((TODAY()-A1)/365)), 5 is X.<br>
=IF((TODAY()-A1)/365>=5,5,<em><strong>((TODAY()-A1)/365)</strong></em>), ((TODAY()-A1)/365) is Y.<br>
<br>
So in English, this says, if the number of years between A1 and today is greater than or equal to 5, give me a 5, if it's less than 5, give me the number of years between A1 and today. Make sense?<br>
<br>
On preview, I'm a her. :Dcomment:ask.metafilter.com,2008:site.106804-1540964Fri, 14 Nov 2008 10:34:37 -0800fuzzbeanBy: inigo2
http://ask.metafilter.com/106804/Excel-date-arithmetic-problem#1540966
I'm not fuzzbean, but I do want to take a break from work:<br>
<br>
<i>=IF((TODAY()-A1)/365>=5,5,((TODAY()-A1)/365))</i><br>
<br>
<i>TODAY()-A1</i> = number of days between today, and the date you're checking.<br>
Divide that by 365 to get the number of years between the dates.<br>
<br>
The IF statement says "If the number of years between the dates is greater than or equal to 5 years, then just say it's 5 years apart. Otherwise, calculate the actual number of years apart and use that."<br>
<br>
But yeah, I would probably go with IAmBroom's because it avoids leap year issues.comment:ask.metafilter.com,2008:site.106804-1540966Fri, 14 Nov 2008 10:34:58 -0800inigo2By: Class Goat
http://ask.metafilter.com/106804/Excel-date-arithmetic-problem#1540992
The magic here is in how dates are stored. They're actually stored as a cardinal number of days since the epoch (i.e. "the start of time" which in this case turns out to be 1900). Formatting a cell as "date" makes Excel convert that number into a year/month/day for display purposes.<br>
<br>
But you can treat that value as a number, and do math on it. Subtracting one date from another gives you the number of days between them.comment:ask.metafilter.com,2008:site.106804-1540992Fri, 14 Nov 2008 10:55:25 -0800Class GoatBy: stefanie
http://ask.metafilter.com/106804/Excel-date-arithmetic-problem#1541105
I'm a big fan of <a href="http://www.cpearson.com/excel/datedif.aspx">DATEDIF</a> in cases like this, so I'd use:<br>
<br>
=MIN(DATEDIF(A1,TODAY(),"y"),5)<br>
<br>
The "y" indicates that you want the results in whole years. You can use "d" or "m" for days or months, and if you want to get really tricky and exclude weekends and holidays you can use <a href="http://www.cpearson.com/excel/DateIntervals.htm">NETWORKDAYS</a>.comment:ask.metafilter.com,2008:site.106804-1541105Fri, 14 Nov 2008 12:17:26 -0800stefanie