year difference in excel - excel

I have two columns of year in excel sheet as year.months format (column first as 1.11 means 1 year 11 months) column second as 0.7 means 7 months). I need difference between two columns as year.months format. i.e., 1.11-0.7=1.4 means 1 year 4 months.

This approach will perform the math by converting everything to the same base units as month. You can perform the steps over multiple column which will make it easier to read, maintain and troubleshoot. When everything is working you can combine it into a single formula which will be a monstrosity that is hard to read and potentially maintain by others in the future.
For the sake of this solution, assume your 1.11 date is in A1 and you 0.7 date is in B1.
Step 1) Convert A1 and B1 to months
In C1 place the following formula and copy it to D1
=LEFT(A1,FIND(".",A1)-1)*12+RIGHT(A1,LEN(A1)-FIND(".",A1))
Step 2) Find the difference in months
In E1 Place the following formula
=C1-D1
Step 3) Convert difference in months back in to year.month format
In F1 place the following formula
=INT(E1/12)&"."&(E1-INT(E1/12)*12)
Now if you got and substitute formulas into one another so its all in a single cell, your formula will wind up looking the following.
=INT(((LEFT(A1,FIND(".",A1)-1)*12+RIGHT(A1,LEN(A1)-FIND(".",A1)))-(LEFT(B1,FIND(".",B1)-1)*12+RIGHT(B1,LEN(B1)-FIND(".",B1))))/12)&"."&(((LEFT(A1,FIND(".",A1)-1)*12+RIGHT(A1,LEN(A1)-FIND(".",A1)))-(LEFT(B1,FIND(".",B1)-1)*12+RIGHT(B1,LEN(B1)-FIND(".",B1))))-INT(((LEFT(A1,FIND(".",A1)-1)*12+RIGHT(A1,LEN(A1)-FIND(".",A1)))-(LEFT(B1,FIND(".",B1)-1)*12+RIGHT(B1,LEN(B1)-FIND(".",B1))))/12)*12)
I would not want to be someone new coming in and looking at that and trying to figure out what its doing let alone editing it 3 months down the road when a change is required.
A few caveats, Make sure the date in A is bigger than the date in B. It gives bad results for negative values. Also ensure there is always a "." in the date or errors will ensue. If there is a 12 month difference it will currently display 1.0 as the difference instead of 0.12. If the later is required, a special case would need to be developed for when there is differences resulting in multiples of 12 differences.

Working with DatedIF
Assume start date is in B1 and end date is in A1.
Step 1) Convert date
Convert the string to an actual excel serial date
In C1 and Use the following formula and copy to D1
=DATE(LEFT(A1,FIND(".",A1)-1),RIGHT(A1,LEN(A1)-FIND(".",A1)),1)
The conversion assumes the first of the month
Step 2) Determine the difference in months
Place the following in E1
=DATEDIF(D1,C1,"M")
Note the start date need to be further back in time than the end date or you will get #NUM error.
Step 3) Convert back to your string format for y.m
Place the following in F1
=INT(E1/12)&"."&(E1-INT(E1/12)*12)
and combined into the uglyness of a single cell formula:
=INT((DATEDIF(DATE(LEFT(B1,FIND(".",B1)-1),RIGHT(B1,LEN(B1)-FIND(".",B1)),1),DATE(LEFT(A1,FIND(".",A1)-1),RIGHT(A1,LEN(A1)-FIND(".",A1)),1),"M"))/12)&"."&((DATEDIF(DATE(LEFT(B1,FIND(".",B1)-1),RIGHT(B1,LEN(B1)-FIND(".",B1)),1),DATE(LEFT(A1,FIND(".",A1)-1),RIGHT(A1,LEN(A1)-FIND(".",A1)),1),"M"))-INT((DATEDIF(DATE(LEFT(B1,FIND(".",B1)-1),RIGHT(B1,LEN(B1)-FIND(".",B1)),1),DATE(LEFT(A1,FIND(".",A1)-1),RIGHT(A1,LEN(A1)-FIND(".",A1)),1),"M"))/12)*12)

Let's say A1=1.11 and B1=0.7 then formula would be:
=LEFT(A1;FIND(".";A1;1)-1)-LEFT(B1;FIND(".";B1;1)-1)&"."&(MID(A1;FIND(".";A1;1)+1;2)-MID(B1;FIND(".";B1;1)+1;2))

Related

Get working day of the month with the lowest value

I have a excel spreadsheet with two columns. One with a date and another with the value.
I want to get the working day of the month with the lowest value.
I tried to use a pivot table for it and then group the date but I get a lot of errors.
You can find the spreadsheet here and the sheet name is Historical. The others are just attempts made by me.
Spreadsheet
Thanks
The formula entered in E2 below
is
=AGGREGATE(15,6,(POWER(10,LOG10(((YEAR(D2)=YEAR($A$2:$A$3254))*(MONTH(D2)=MONTH($A$2:$A$3254)))))*$B$2:$B$3254),1)
and the array formula entered in F2 below is
=INDEX($A$2:$A$3254,MATCH(YEAR(D2)&MONTH(D2)&E2,YEAR($A$2:$A$3254)&MONTH($A$2:$A$3254)&$B$2:$B$3254,0))
I suggest to make an triple nested if-construct that checks if the weekday of the date is a workday, or the date+ 1 or the day +2. Assuming the date is in cell A4
= if(instr(weekday(A4),”23456”)>0, A4,
if(instr(weekday(A4+1),”23456”)>0, A4 + 1,
if(instr(weekday(A4+2),”23456”)>0, A4 + 2,”cannot happen”)))
Explanation: one of 3 consecutive days is always a working day.
There may be typos since I edit that on iPad without Excel available to test.
Weekday returns 1 for Sunday and 7 for Saturday. So 2-6 are workdays.
However with that simple approach you will not detect public holidays on a working day if that is a problem.
Hope I understood you question correctly. One data example with solution would have explained it better.

Change cell color of cell if within 3 days of cell date

I am attempting to make a cheat-sheet for paperwork that needs to be submitted at a specific date. Cell A8 is the given date; cells B8-F8 have formulas to generate dates based on A8's given date. I want cells B18-F8 to turn red 3 days before the generated dates, and stay that way until the due date passes (for now).
I have tried: =$FD8<=FD8 - (3) with every variation of "<,>,<="
Most solutions me to use "TODAY()" but I specifically want it to go off of the given date.
Can anyone point me in the right direction? Thanks!
It would be most convenient to use another set of cells to generate the B8-F8 dates and to substract three days. For example, B9-F9. Then you can use conditional formatting to format B8-F8 based on the B9-F9 values.
To substract days you can generate a VBA code that creates a custom function.
Function myDateAdd(interval, howmany, adate)
myDateAdd = DateAdd(interval, howmany, adate)
End Function
Or write a more elaborate formula
=IF(
DAY(B8)>3,
DATE(YEAR(B8),MONTH(B8),DAY(B8)-3),
DATE(YEAR(B8),MONTH(B8)-1,(DAY(B8)-31)*-1)
)
You still need to write something extra to verify that the B8 date has 31 (or 30 or 28) days. Combine IF() and ISDATE() multiple times to achieve this.

Extract date out of cell with date and time

I have a cell G4 with date and time in a format (Text string):
1/29/2020 1:34:24 PM
I need to convert it to DATE formatted cell. How to do that?
I have tried to get numbers and convert them to DATE with this formula:
=DATE((MID(G4;SEARCH("/";G4)+4;SEARCH("/";G4;SEARCH("/";G4)+1)-SEARCH("/";G4)+2));(MID(G4;SEARCH("/";G4)+1;SEARCH("/";G4;SEARCH("/";G4)+1)-SEARCH("/";G4)-1));(LEFT(G4;FIND("/";G4;1)-1)))
So:
I am extracting year:
=MID(G4;SEARCH("/";G4)+4;SEARCH("/";G4;SEARCH("/";G4)+1)-SEARCH("/";G4)+2)
Month
=MID(G4;SEARCH("/";G4)+1;SEARCH("/";G4;SEARCH("/";G4)+1)-SEARCH("/";G4)-1)
Day
=LEFT(G4;FIND("/";G4;1)-1)
I am getting as a result:
1.5.2022
I need it as it is now, but output should be 29.1.2020 in this case. Later I want to get day difference two that way formatted dates. Is it possible to do it with formula without performing any other cell formatting operations?
EDIT:
I got it working, the only problem is:
How to extract number (year) after third "/"? My current formula is not correct:
=MID(G4;SEARCH("/";G4)+4;SEARCH("/";G4;SEARCH("/";G4)+1)-SEARCH("/";G4)+2)
It does not function correct in this case:
2/5/2020 12:21:05 PM
EDIT:
I did it this way (I also had to minus G2 - F2, to get days difference):
=IFERROR(DAYS(MID(G2;SEARCH("/";G2)+1;SEARCH("/";G2;SEARCH("/";G2)+1)-SEARCH("/";G2)-1)&"."&LEFT(G2;FIND("/";G2;1)-1)&"."&MID(G2;FIND("/";G2;FIND("/";G2)+1)+1;4);MID(F2;SEARCH("/";F2)+1;SEARCH("/";F2;SEARCH("/";F2)+1)-SEARCH("/";F2)-1)&"."&LEFT(F2;FIND("/";F2;1)-1)&"."&MID(F2;FIND("/";F2;FIND("/";F2)+1)+1;4));"")
You probably need to replace an order of day.month.year and "." to "/" if you are using different date setting (region). I have one setup, so this seems to work.
FYI DATES in excel are stored as integers. They represent the number of days since 1900/01/01 with that date being 1. TIME is stored as a decimal representing fractions of a day or 24 hours. 0.5 represents noon. 24:00 is not an officially supported time in excel, but will work with some functions.
The DATE Formula is looking for three arguments representing YEAR, MONTH, DAY in that order.
DATE(Year, Month, Day)
You need to pull the text from your string representing these values. I find it easiest to pull each one individually in its own cell to ensure the part of the formula is working first then copy and past that part into the DATE formula so the whole calculation in the end can be performed in one cell.
YEAR
To get the year use the following formula:
MID(G4,FIND("/",G4,FIND("/",G4)+1)+1,4)
MONTH
To get the month use the following formula:
LEFT(G4,FIND("/",G4)-1)
DAY
To get the day use the following formula:
MID(G4,FIND("/",G4)+1,FIND("/",G4,FIND("/",G4)+1)-FIND("/",G4)
COMBINED FORMULA
Place the above formulas into the date formula as follows:
=DATE(MID(G4,FIND("/",G4,FIND("/",G4)+1)+1,4),LEFT(G4,FIND("/",G4)-1),MID(G4,FIND("/",G4)+1,FIND("/",G4,FIND("/",G4)+1)-FIND("/",G4)-1))
Note the only cell reference in the formula is G4. The results of the formula are not in an Excel Date format. Change the formatting of your cell to meet your needs. In your case I would apply a custom cell format of d.m.yyyy
If you have TEXTJOIN,
=TEXTJOIN("/",TRUE,INDEX(FILTERXML("<a>,<b>"&SUBSTITUTE(SUBSTITUTE(TEXT(A1,"dd/mm/yyyy hh:mm:ss"),"/","</b><b>")," ","</b>",1)&"</a>","//b"),N(IF({1},{2,1,3}))))
Depending on your version it may need to be confirmed with Ctrl-Shift-Enter instead of Enter when exiting edit mode.
the reason the second did not work is that Excel actually changed it to a date and a date is a double, not text. So there are no / in the data. so we need to force back to the incorrect string.
Those for whom the TEXTJOIN function is not available can use this:
=DATE(FILTERXML("<DATA><A>" & SUBSTITUTE(SUBSTITUTE(A1;"/";"</A><A>");" ";"</A><A>") & "</A></DATA>";"/DATA/A[3]");FILTERXML("<DATA><A>" & SUBSTITUTE(SUBSTITUTE(A1;"/";"</A><A>");" ";"</A><A>") & "</A></DATA>";"/DATA/A[1]");FILTERXML("<DATA><A>" & SUBSTITUTE(SUBSTITUTE(A1;"/";"</A><A>");" ";"</A><A>") & "</A></DATA>";"/DATA/A[2]"))

Getting minutes between different dates in excel

I'm trying to calculate the time between the dates , at the beginning the formula
was working fine but I've noticed that it does not work when the date is different
For example , I have the following information on cell A1: 09/15/2016 10:00 AM
On Cell B2 I have: 09/16/2016 10:00 AM
The formula is just B2-A1 but instead of giving me a result of 24 hours is just giving me 0 . I believe the formula is not recognizing that these are 2 different days and is just doing 10-10
Any idea how to fix this ?
I was able to get the result 24 by setting a custom format of [h] (you will have to type it into the 'Type:' box) on cell C1 while using the formula =B1-A1
Excel Reference
'Format Cells' view
The problem with just using =B1-A1 is that if either or both of those cells is not populated then you will get weird numbers in C1. You may want to make C1 display as a blank cell unless both boxes are populated, try something like this =IF(OR(ISBLANK(A1),ISBLANK(B1)),"",B1-A1)
The reason for the weird numbers is that Excel calculates time based on a predefined decimal system that indexes time starting at like 1/1/1900 or something like that. So when manipulating or calculating time, that is something that you always have to keep in the back of your mind.
Hope this helps.
Formation the destination cell to will do but since you have date and time combined it will show as 1 calendar day difference 0 only means that 12 am after the 1 day difference, I know it does not make any sense but its Excel...
If I was you, on column A, I would add the date, and on Column B, the time.
then just work with the time, as both combined can be tricky
Don't forget to format your cells!! (right click>Format Cells>Time>3/14/12 1:30 PM)

Trying to calculate a renewal date in excel

Have a few different items with different rental periods ( 1,3,6 months to 1,3,5 years). As it stands I have a column with the last renewal date and a column indicating what is the renewal period. I wanted excel to calculate what the Renewal date would be based on the selected type of renewal. My first attempt was to convert the renewal types to some kinda of similar denomination but i got stuck trying to figure out what value/format to use.
Last renewal date Renewal Type Renewal Date
11/11/2013 1 Year
2/14/2014 2 Years
8/28/2011 5 Years
11/27/2013 3 Months
[Excel stores dates as floating point numbers. They advance by 1 each day and the fractional part holds the day fraction, e.g. 0.5 is midday. The exact value is user-configurable between 1900 and 1904 origins which can complicate things. Note that in the 1900 origin, date 60 (corresponding to the non-existent date 29-Feb-1900) is defined. Leap seconds are not implemented.]
If you can tolerate the second column being integral months, then use the EDATE function.
For example, if your table is orientated on cell A1, use
=EDATE(A2, B2) in cell C2 and copy downwards.
I would use a Vlookup in the third column which should point to a table which has conversion of 1 year/6mos into days. (365,180, ect). Then just add that vlookup to the original date.
So
`TblDate
Lookup Days
1mo 30
.. ..`
=VLOOKUP(B1,TblDate,2,FALSE)+A1
Maybe try:
=IF(ISERROR(FIND("year",B2)),DATE(YEAR(A2),MONTH(A2)+LEFT(B2,1),DAY(A2)),DATE(YEAR(A2)+LEFT(B2,1),MONTH(A2),DAY(A2)))
In D2 enter:
=--MID(B2,1,FIND(" ",B2))
and copy down
In E2 enter:
=MID(B2,FIND(" ",B2)+1,1)
and copy down
Finally in C2 enter:
=IF(E2="Y",DATE(YEAR(A2)+D2,MONTH(A2),DAY(A2)),DATE(YEAR(A2),MONTH(A2)+D2,DAY(A2)))
Format C2 as Date and copy down. For example:
.
.
There is a way to convert your second column into pure month format, using Excel:
=IF(COUNTIF(B2,"*Year*"),(TRIM(LEFT(B2,2)))*12,IF(COUNTIF(B2,"*Month*"),TRIM(LEFT(B2,2)),NA()))
This can then be used with EDATE(). For example, lets say that the above code is in cell C2, it would produce 12. Then, you could in cell D2, you could put
EDATE(A2,C2)
in there. The code works for anything between 1 month and 99 years and doesn't care whether the first number is one or two digits and whether the word is month, months, year or years - it will work with all of it, as TRIM() removes whitespace (thus allowing for one character and a space or two characters) and * is a wildcard (hence allowing x years or xz year).
For EDATE to work you need to make sure the number format is set to date type.
Note: an extra cell for the months will be needed - so it might require a little rearranging, but using CTRL+C or copy cells tool in the bottom right of a selected cell will easily copy it, and for each one it will automatically convert it.
create a new column and type in the formaula box
=B2+1095
to get your renewal date
1 year is 365
2 years is 730
3 years is 1095
and so on and so forth.
this was the most simple way and worked for me!

Resources