Excel公式,以获得年月差异,但整整12个月

excel formula to get years and months difference but in full 12 months

提问人:KRStam 提问时间:8/30/2023 更新时间:9/1/2023 访问量:45

问:

我有这个公式

    =IF(DATEDIF($C7; H$3; "y")=0; "";
    IF(DATEDIF($C7; H$3; "y")=1; "1 year";
        DATEDIF($C7; H$3; "y") & " years")) &
IF(DATEDIF($C7; H$3; "ym")=0; "12 months";
    IF(DATEDIF($C7; H$3; "ym")=12; "; 12 months";
        IF(DATEDIF($C7; H$3; "ym")=1; "; 1 month";
            "; " & DATEDIF($C7; H$3; "ym") & " months")
    )
)

有没有办法说 17 年;17年后12个月;11 个月而不是 18 年;12个月?

enter image description here

Excel 公式 DateDiff

评论

1赞 Solar Mike 8/31/2023
尝试 year() 和 month(),因为我从未遇到过这个问题。

答:

1赞 taller 8/31/2023 #1

我不知道为什么你的公式中的所有分隔符都是(例如)。它应该在 Excel 或 GoogleSheet 上。;DATEDIF($C7; H$3; "y"),

以下公式在 Excel 365 上进行了测试。

=IF(DATEDIF($C7,H$3,"y")=0,"",IF(DATEDIF($C7,H$3,"y")=1,"1year",IF(DATEDIF($C7,H$3,"ym")=0,(DATEDIF($C7,H$3,"y")-1)&"years",DATEDIF($C7,H$3,"y")&"years")))&IF(DATEDIF($C7,H$3,"ym")=0,"; 12months",IF(DATEDIF($C7,H$3,"ym")=12,"; 12months",IF(DATEDIF($C7,H$3,"ym")=1,"; 1month","; "&DATEDIF($C7,H$3,"ym")&"months")))

enter image description here

LET公式更易于理解和维护。

=LET(
m,DATEDIF($C7,H$3,"ym"),
y,DATEDIF($C7,H$3,"y"),
mm,if(m=0,12,m),
yy,if(m=0,y-1,y),
mo,if(mm=1,"month","months"),
yr,if(yy=1,"year","years"),
CONCAT(yy,yr,"; ",mm,mo))

评论

0赞 KRStam 8/31/2023
现在是 18 年;12 个月,但更新应为 17 年;12个月。我的意思是相反
0赞 taller 8/31/2023
明白了。我已经更新了公式。
0赞 basic 8/31/2023 #2

另一种选择:

=LET(yrs;DATEDIF($A2;B$1;"m")/12;
     sy;IF(INT(yrs)=1;"";"s");
     zerom;INT(12*(yrs-INT(yrs)))=0;
     sm;IF(ROUND(12*(yrs-INT(yrs));0)=1;"";"s");
     INT(yrs)-zerom & " year" & sy & " " & 
   ROUND(12*(yrs-INT(yrs));0)+(12*zerom) & " month" & sm)

enter image description here