Issue with DATEDIF function - excel

DATEDIF function giving different values as shown in the snapshot.
Why this?

First of all, you should avoid passing D2 directly to DATEDIF formula.
DATEDIF requires first 2 arguments as a date. You pass 0 and D2 (C2-B2). Since lowest possible date in excel is 01-Jan-1900 and it is = 1, your arguments are 0 = 0-Jan-1900 (non existing date) and 546 = 29-Jun-1900. You would get more errors in calculation if your dates would include leap year (1900 is not leap year)
Also consider looking at Microsoft as it states there are issues with "md" argument (see "Known issues").
So correct formula would be:
=DATEDIF(B2,C2,"y")&" years "&DATEDIF(B2,C2,"ym")&" months "&DATEDIF(B2,C2,"md")&" days"
Result:

Related

Excel difference between dates not working

I have the following data in columns:
A B
1 1/08/2021 29/11/2021
These values are the result of some searches from other columns with this formula:
=MINIFS('sheet2'!$G:$G,'sheet2'!$F:$F,$E1)
Both the source cells and the resulting cells are of type date
I then look to get the DATEDIF between these and I end up with #NAME?
=DATEDIF($A1,$B1,”M”) /* Corrected the above "W" to "M"... typo! */
I tried DATEVALUE on the search result cells like this:
=DATEDIF(DATEVALUE($A1),DATEVALUE($B1),”M”)
But I end up with #VALUE? so the resulting values from the searches are not seen as dates. What have I missed? Thanks!
The "W" on =DATEDIF($A1,$B1,”W”) is not a valid unit, accordingly to Microsoft's documentation (here).
The list of valid units is:
"Y" : The number of complete years in the period.
"M" : The number of complete months in the period.
"D" : The number of days in the period.
"MD": The difference between the days in start_date and
end_date. The months and years of the dates are ignored. Not
recommended, as there are known limitations with it.
"YM" : The difference between the months in start_date and end_date. The days
and years of the dates are ignored
"YD" : The difference between the
days of start_date and end_date. The years of the dates are ignored.
You have two mistakes:
The #NAME? error is because you are using left and right quotation marks instead of straight quotation marks. eg: “W” instead of "W".
If you correct that, you will see a #NUM! error because W is not a valid argument for the function.
See MS Help for the DATEDIF function for more information as well as a warning about the MD unit.

Formatting date in Excel

How can I easily format "202104" --> 2021/04 in Excel?
My current method is to concatenate the original string with "01" and then change it into a date. However, I am seeking a more efficient format method.
Thanks
Try this.
For Text
=LEFT(A1,4)&"/"&RIGHT(A1,2)
For Date Value
=TEXT(DATEVALUE(LEFT(A1,4)&"/"&RIGHT(A1,2)),"YYYY/MM")
As far as I know, 2021/04 is not a valid date in Excel, but 2021/04/01 (first of April, year 2021) is.
In order to achieve this, you can use this formula:
=DATE(INTEGER(202104 / 100);MOD(202104;100);1)
Where:
1) INTEGER(202104/100) is the integer division of 202104 by 100, calculating the year.
2) MOD(202104;100) means 202104 modulo 100, in order to calculate the month.
3) 1 means the first day of the month.

Why is the Excel DateDif function returning 0 months for dates between "4/30/2020" and "3/31/2020"?

Microsoft says the DateDif function: =DATEDIF(A3,B3,"M") returns the full number of months between those two dates assuming A3 = Starting month and B3 = Ending month.
However, for dates between 4/30/2020 and 3/31/2020, I am getting a value of 0. I was expecting to get a value of 1. DateDif between 3/1/2020 and 5/31/2020 returns 2, while datedif between 4/30/2020 and 5/31/2020 returns 1 - both of which makes sense to me. So why is the DATEDIF between 4/30/2020 and 3/31/2020 returning 0?
one can create their own version and use vba's DateDiff:
Function MyDateDif(srt As Date, ed As Date, str As String) As Long
MyDateDif = DateDiff(str, srt, ed)
End Function
Then you would use it:
=MyDateDif(A3,B3,"M")
Which returns 1 as it should
You could use ROUNDDOWN and YEARFRAC to calculate the result. This will return a decimal and round down to the nearest whole number. I guess the moral of the story is to not use deprecated functions.
=ROUNDDOWN(YEARFRAC(A3,B3)*12,0)

use IF() with TIMEVALUE() in excel

I have the folowing formula:
=IF(TIMEVALUE("14:30") - TIMEVALUE(NOW()) < TIMEVALUE("00:00"),"Past","Future")
Excel is giving me ERROR, i checked all formulas individually and they all give me the time value (which in theory should be enough to compare with an IF statement).
How come that i keep on getting error. Some cell formats not correct or something? Any help is appreciated!
Try to evaluate the formula step by step to see, if this is what you want.
Working formula:
=IF(TIMEVALUE("14:30") - NOW() < TIMEVALUE("00:00");"Past";"Future")
Evaluated arguments, using [F9 key]:
=IF(0,604166666666667 - 42719,6943635416 < 0;"Past";"Future")
You will always get the "Past" as returned value.
Explanation of date and time in Excel
The TIME in Excel is a proportion of a day. "00:00" = 0.00 and "24:00" = 1.00. Other values of TIME are DECIMALS between 0 and 1.
The DATE is a number of days since the first day. "1900-01-01" = 1 and "2000-01-01" = 36526. It is always an INTEGER.
Combining DATA and TIME (like in NOW() function) gives you an INTEGER + DECIMAL. When I evaluated the NOW() it returned 42719,6943635416
The TIMEVALUE function expects a STRING/TEXT that is considered a TIME and converts that STRING/TEXT to DECIMAL between <0; 1>.
Links
MS - How to use dates and times in Excel
Trump-Excel:Identify Errors Using Excel Formula Debugging (2 Methods)
Convert the Now to string first, using Text function, then it will work as expected.
TEXT(NOW(),"HH:MM:SS")
Final formula:
=IF((TIMEVALUE("11:30") - TIMEVALUE(TEXT(NOW(),"HH:MM:SS"))) < TIMEVALUE("00:00"),"Past","Future")

VBA equivalent of DATEVALUE?

The formula function of =DATEVALUE and VBA's DateValue() provide exclusive results. How can I format a date in VBA to obtain the same serialization as I would by using =DATEVALUE?
Example: 1/19/2016, formatted "m/d/yyyy", will return 42388 through =DATEVALUE()
Use CLng e.g.
=DATEVALUE("7/7/16")
Returns
42558
In VBA:
? CLng(DateValue("7/7/16"))
42558
Several ways to do this. #Robin has a good one. I'll add one more
?DateDiff("d",0, "7/7/16")
42558
The DateDiff function calculates the difference between the two dates (2nd and 3rd parameters) in the units of the first parameter. So if you changed "d" to "h" you would get the number of hours between 0 (the first possible date in Excel/VBA) and 7/7/2016

Resources