Difference between two dates with decimal result - excel

I want to get the difference between two dates, but I want the result to be in decimal numbers, for example if the difference is (1 Year and 6 Months) I want the result to be (1.5) which means Year and a half!

There is a specific function which does this in excel - YEARFRAC function - it calculates slightly differently according to the 3rd argument, see excel help for more but this is the basic version
=YEARFRAC(A2,B2,1)
Where A2 is the start date and B2 the end date. This is accurate to the day, I assume that's what you want

Related

How to convert custom digits to date in excel

Assuming 421 stands for April 2021 and 1299 for December 1999, what would be the correct formula to convert the digits in to the corresponding date format in excel?
Adjust the 30 as you see fit:
=IFERROR(DATE(IF(VALUE(RIGHT(A1,2))<30,2000+RIGHT(A1,2),RIGHT(A1,2)),LEFT(A1,LEN(A1)-2),1),"")
Actually, here is a one-liner (note, to get the correct century the formula is posted lower in the answer and is more complicated):
=DATE(RIGHT(A1,2),LEFT(IF(LEN(A1)=3,0&A1,A1),2),1)
DATE requires 1) Year 2) Month and 3) Day
So, it's easy to get the year (just take the two most digits)
However, it's trickier to get the month, since it can be 3 or 4 digits, so you need some if logic to add a 0 in front if only 3 digits and THEN take the left two digits to extract the month.
Finally, you have to have a day for the formula, so just put a 1. Then format the cells as desired to whatever date format. See screenshot.
In order to get the correct century, then the formula gets complicated :) Basically, it says if the date is greater than now, subtract a century. If it is less than now, then go with that date.
=IF(DATE(YEAR(DATE(RIGHT(A1,2),LEFT(IF(LEN(A1)=3,0&A1,A1),2),1))+100,MONTH(DATE(RIGHT(A1,2),LEFT(IF(LEN(A1)=3,0&A1,A1),2),1)),DAY(DATE(RIGHT(A1,2),LEFT(IF(LEN(A1)=3,0&A1,A1),2),1)))>NOW(),DATE(RIGHT(A1,2),LEFT(IF(LEN(A1)=3,0&A1,A1),2),1),DATE(YEAR(DATE(RIGHT(A1,2),LEFT(IF(LEN(A1)=3,0&A1,A1),2),1))+100,MONTH(DATE(RIGHT(A1,2),LEFT(IF(LEN(A1)=3,0&A1,A1),2),1)),DAY(DATE(RIGHT(A1,2),LEFT(IF(LEN(A1)=3,0&A1,A1),2),1))))

Finding a shorter period of days within a longer period for calculation

I have a set of dates, in total 7 periods spanning 6 months each with corresponding calculation factor.
I will have user input of period for which they'd want the calculation to be done, which can fall within one of the 6 months periods or encompass between two or more such periods wholly or partially.
Illustration:
The preset periods:
User input:
I've obtained decimal value (monthly basis) of the periods input by the user for calculation. For first instance (see user input), the decimal value for the period 01-01-2015 to 29-04-2015 will be 3.97 on monthly basis. The calculation for that period would be like:
n*3.97*113%
For the second instance, the decimal value for the period 30-04-2015 to 30-06-2015 would be 2.03, which would be used to do calculation at 113% and then the result will be added to the calculation done at 119% using the decimal value 5.65 for the period 01-07-2015 to 20-12-2015:
(n*2.03*113%)+(n*5.65*119)
Think I can handle the breaking up of periods since the revision event is bi-annual on particular dates but advice regards to that is welcome. More importantly, I need help tracing the preset calculation factor (say 132%) corresponding to the period input by the user as illustrated above. Is it feasible?
I will use the standard approach for finding the overlap between two dates and will split the task into three parts as in my answer to this recent question.
(1) The first part is finding the overlap between the user's range of dates and one or more revision periods in whole months and will need an array formula. I have chosen to use the Datedif function 1 to get the difference in months between the beginning and end of the overlaps. If there is no overlap, the start date fed in to Datedif will be after the end date, and it will return an error which can be trapped by Iferror. If the user's dates start in A2 and B2, this gives in C2:
=SUM(IFERROR((DATEDIF(IF(K$2:K$8>A2,K$2:K$8,A2),IF(L$2:L$8<B2,L$2:L$8,B2),"m")+1)*M$2:M$8,0))
which has to be entered as an array formula using CtrlShiftEnter
The above result includes the first and last months entered by the user even if they are incomplete months. It's then necessary to subtract any missing days in the first and last months.
(2) Missing days in first month as a fraction of the number of days in that month in D2:
=SUMIFS($M$2:$M$8,$K$2:$K$8,"<="&A2,$L$2:$L$8,">="&A2)*(A2-EOMONTH(A2,-1)-1)/(EOMONTH(A2,0)-EOMONTH(A2,-1))
A noted by OP, this could also have been done using sumproduct, vlookup or index/match.
(3) Missing days in last month as a fraction of days in that month in E2:
=SUMIFS($M$2:$M$8,$K$2:$K$8,"<="&B2,$L$2:$L$8,">="&B2)*(EOMONTH(B2,0)-B2)/(EOMONTH(B2,0)-EOMONTH(B2,-1))
The total is just (1)-(2)-(3) or
=C2-D2-E2
I have put the results of OP's two examples for comparison in H2 and H3: my results agree with them in the first 3 significant figures.
n*3.97*113%
(n*2.03*113%)+(n*5.65*119)
In all cases I have set n=1 and ignored the fact that the rate is a percentage.
This shows how the results would be calculated manually:
1 Pros of using Datedif:
(1) Works across year boundaries unlike just using Month function.
(2) Works conveniently with Iferror to identify non-matching date ranges.
Cons of using Datedif:
(1) It is an undocumented function and may be withdrawn in future.
(2) In this particular case, all date calculations are within the same year so Month would be useable.

Excel - Want to compare a projected date against a target date

In Excel, I am trying to compare a forecast date against a target date and state in the 3rd column the difference, I have searched for a formula to put in the 3rd column such as DATEDIF (F5, G5,"m") but this is not able to state the difference if the projected date is less than the target date,
Please see example below:
EXCHANGE Target Date EXCHANGE Forecast Date EXCHANGE Difference
01/12/2018 11/01/2019 1
01/08/2017 16/02/2019 18
02/08/2017 01/06/2017 #NUM!
Use some Error handling, if the date is in the past, flip the calculation and multiply it by -1 or subtract it from 0. It's annoying that the function doesn't handle this itself but oh well...
IFERROR(DATEDIF(F5,G5,"m"),DATEDIF(G5,F5,"m")*-1)
IFERROR(DATEDIF(F5,G5,"m"),0-DATEDIF(G5,F5,"m"))
EDIT:
To evaluate weeks you would use the same function but taking the difference in days, you can then divide the result by 7 (make use of the ROUND() function if you don't want a decimal answer)
IFERROR(DATEDIF(F5,G5,"D")/7,0-DATEDIF(G5,F5,"D")/7)
IFERROR(ROUND(DATEDIF(F5,G5,"D")/7,0),ROUND(0-DATEDIF(G5,F5,"D")/7,0))

Convert date e.g. Jun 05 2016 08:00:00 to dd/mm/yyyy hh:mm:ss

Got input such as in the topic title.
Trying to figure out how to convert this into UK date and time to be used in calculations.
I've looked at some methods on Google such as using text to columns, but I don't think this is what I'm looking for...
Thanks!
Edit: month is always in abbreviated format.
Edit 2: I should mention that I'm in the UK, and it doesn't seem to convert US date automagically.
Edit 3: Data:
Jun 05 2016 08:00:00 to dd/mm/yyyy hh:mm:ss
Assuming that your source date is a string and it is in the cell D10, the first thing you need to do is convert it to an excel date time serial. In the Excel Date Time serial there are a few things to note.
The integer portion of the number represents the number of days since Jan 1 1900 in windows and I think 1904 on mac
The decimal portion of the number represents the time in fraction of a day. 0.5 would represent noon. Valid Excel times for VBA are 00:00 to 23:59:59. 24:00 is not a valid time, though it will work with some excel formulas
So in order to convert your string to an Excel date serial we will need to rip out the components and dump them into the DATE() function. The date function consists of three arguments:
DATE(year,month,day)
Pretty straight forward with the exception that those values need to be numbers. Why dont we start pulling your information going from the largest unit to the smallest unit.
Thankfully your string is of consistent length. You have leading 0 for your single digits so they will occupy the same space as double digits. So this method will work until the year 9999, but I don't think we are too worried about that right now.
In order to pull the year we look at where it is in your string and how long it is. So by simply counting we know it starts in the 8th character position and its 4 characters long. We use this information with the MID() function
=MID(D10,8,4)
In order to pull the month it get a little more complicated since we need to convert it from an abbreviation to a number. There are several ways of doing this. You could go for a long IF statement which would wind up repeating the pull of the month a 11 times. Instead I decided to use the MATCH() function and built an array of month abbreviations inside it. The MATCH() function will return the number/position of what you are searching for in the provided search list. So as long as we enter the months in chronological order, their position will correspond to their numeric values. As such our formula will look like:
MATCH(LEFT(D10,3),{"JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"},0)
The LEFT() function was used to pull the month abbreviation from your string. The 0 at the end tell match to look for an exact match. Important to note, this match method is not case sensitive.
Now to get the day we employ the same principals that we did for pulling the year and we wind up with:
=MID(D10,5,2)
We can now substitute each of the formulas for Year Month and Day into the DATE() function and we will get the date portion of the excel date serial. The formula should look like the following:
=DATE(MID(D10,8,4),MATCH(LEFT(D10,3),{"JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"},0),MID(D10,5,2))
Now you need to tack on your time portion or figure out the decimal portion. In order to do this I would first recommend trying the TIMEVALUE() function. Since time formats tend to be a lot more standard in format than dates, there is a much higher probability that it will work for you. In order to use TIMEVALUE(), the time portion needs to be ripped from the string. This can easily be done with the RIGHT() function as follows:
=RIGHT(D10,8)
That will give you just the time portion which can then be substituted into the TIMEVALUE() function and looks like:
=TIMEVALUE(RIGHT(D10,8))
If the TIMEVALUE() function does not work for you, then you will need to strip out the hour minutes and seconds and dump their results in to the TIME() function. Do this in the same way you pulled the year and the day for the DATE() function. Just update your character counts. TIME() uses three arguments as follows:
TIME(HOUR,MINUTES,SECONDS)
Now that you have figured out your date portion and your time portion all you need to do is add them together to get all the information into one cell. The resulting formula will look like:
=DATE(MID(D10,8,4),MATCH(LEFT(D10,3),{"JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"},0),MID(D10,5,2))+TIMEVALUE(RIGHT(D10,8))
Where ever you windup placing that formula, remember to change the formatting on the cell to a custom date. Enter the cell custom format as in the image below.
If you have a list of date times to convert in a column, simply copy your formula and formatted cell down as far as you need to go.
Proof of Concept
Formulas used
For more information on the functions used in the formulas above, follow the links below:
MATCH
DATE
TIMEVALUE
RIGHT
MID
LEFT
{"JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"}
The { } are used to build a custom list or static array.
If you don't want to write a macro in VBA, a cell formula would work if the format is as you specified:
I am assuming the data is in cell B3
=MID(B3,5,2) & "/" & IF(LEFT(B3,3)="Jan","01",IF(LEFT(B3,3)="Feb","02",IF(LEFT(B3,3)="Mar","03",IF(LEFT(B3,3)="Apr","04",IF(LEFT(B3,3)="May","05",IF(LEFT(B3,3)="Jun","06",IF(LEFT(B3,3)="Jul","07",IF(LEFT(B3,3)="Aug","08",IF(LEFT(B3,3)="Sep","09",IF(LEFT(B3,3)="Oct","10",IF(LEFT(B3,3)="Nov","11","12"))))))))))) & "/" & RIGHT(B3,13)
The DATEVALUE function expects a comma between the day and year; the REPLACE function can add that in. The TIMEVALUE function should be able to read that time without modification.
=DATEVALUE(REPLACE(LEFT(A1, 11), 7, 0, ","))+TIMEVALUE(RIGHT(A1, 8))
Note that the original in A1 is left-aligned; this indicates a text value. The converted date/time in B1 is right-aligned; this indicates a true date/time value.
Column B was formatted as dd/mm/yyyy hh:mm:ss. As General it would show as 42526.33333.

Excel Subtracting periods from specific dates using 360-day calendar?

Got formulas for figuring my differences between date periods in days as follows:
=IF(F7="","0",DAYS360(F6,F7)+1)
This gives the days result for each period that I am interested in, but I then need to add each period of days together and subtract them from a current date (like doing service computation). The issue is that I need to do this second calculation within a 360-day calendar as well. If I just try to do a days360() formula with one value being the current date and the other being the total number of days that I need to backtrack, then the "original" date that it comes up with is drastically off.
This seems to be the equivalent of the difference between NETWORKDAYS and WORKDAY functions. The former counts working days between two dates, the latter adds working days to a date, you essentially want the WORKDAY equivalent for DAYS360, which I don't think exists.
You can manipulate DAYS360, though, e.g. with a date in A2 and number of days to subtract (in 360 day mode) in B2 you can use this formula for the date
=A2-B2-MATCH(B2,INDEX(DAYS360(A2-B2-ROW(INDIRECT("1:"&CEILING(B2/30,1))),A2),0),0)

Resources