以下公式适用于大多数日期组合:
=IF(INT(B2)=INT(NOW()),"Today",IF(DAYS(NOW(),B2)>365,IF(DAYS(NOW(),B2)/365.25>1,YEAR(NOW())-YEAR(B2),0)&" years, ","")&IF(IF(MONTH(NOW())-MONTH(B2)<0,MONTH(NOW())-(MONTH(B2)-11),IF(DAYS(NOW(),B2)<28,0,MONTH(NOW())-MONTH(B2)))=0,"",IF(MONTH(NOW())-MONTH(B2)<0,MONTH(NOW())-(MONTH(B2)-11),IF(DAYS(NOW(),B2)<28,0,MONTH(NOW())-MONTH(B2)))&" months and ")&IF(DAY(NOW())-DAY(B2)<0,VLOOKUP(MONTH(B2),{1,31;2,28;3,31;4,30;5,31;6,30;7,31;8,31;9,30;10,31;11,30;12,31},2)-DAY(B2)+DAY(NOW()),DAY(NOW())-DAY(B2))&" days")
除非B2 16/02/2015 15:19:00 且NOW()是 08/02/2016 14:54 ,其结果是 20天但显然应该 11个月和30天。 我努力在我的逻辑中找到漏洞。
答案 0 :(得分:1)
你的功能似乎是使用朱利安年份长度,而不是更复杂的格里高利长度。这就是为什么你的功能的稳健性会受到质疑的原因之一。
幸运的是,Excel有一个你可以使用的内置函数DATEDIF
。但是,出于某种原因,它是一个隐藏的功能! (我完全不知道为什么)。
如果A1和A2包含两个日期,请使用
=DATEDIF(A1,A2,"y") & " years, " & DATEDIF(A1,A2,"ym") & " months, " & DATEDIF(A1,A2,"md") & " days"
A1必须早于A2。
答案 1 :(得分:1)
由于DATEDIF(date1,date2,&#34; md&#34;)在
时基本上生成随机数DAY(dates2)<DAY(date1)
这里有一个可能的公式解决方案(我的开始和结束日期在A2和B2中): -
年
=DATEDIF(A2,B2,"y")
月
=DATEDIF(A2,B2,"m")
天
=IF(DAY(B2)>=DAY(A2),DAY(B2)-DAY(A2),DAY(B2)-DAY(A2)+DAY(EOMONTH(A2,0)))
以下是一些测试用例: -
答案 2 :(得分:0)
对于其他想要做同样事情的人来说,这是我的工作职能:
=IF(DAYS(NOW(),B2)>0,CONCATENATE(IF(DATEDIF(B2,NOW(),"y")=0,"",DATEDIF(B2,NOW(),"y")),IF(DATEDIF(B2,NOW(),"y")=0,"", " year"),IF(DATEDIF(B2,NOW(),"y")>1,"s",""),IF(AND(DATEDIF(B2,NOW(),"y")>0,DATEDIF(B2,NOW(),"ym")>0),IF(IF(AND(DATEDIF(B2,NOW(),"md")>0,OR(DATEDIF(B2,NOW(),"y")>0,DATEDIF(B2,NOW(),"ym")>0))," and ","")=""," and ",", "),""),IF(DATEDIF(B2,NOW(),"ym")=0,"",DATEDIF(B2,NOW(),"ym")),IF(DATEDIF(B2,NOW(),"ym")=0,"", " month"),IF(DATEDIF(B2,NOW(),"ym")>1,"s",""),IF(AND(DATEDIF(B2,NOW(),"md")>0,OR(DATEDIF(B2,NOW(),"y")>0,DATEDIF(B2,NOW(),"ym")>0))," and ",""),IF(DATEDIF(B2,NOW(),"md")=0,"",DATEDIF(B2,NOW(),"md")),IF(DATEDIF(B2,NOW(),"md")=0,"", " day"),IF(DATEDIF(B2,NOW(),"md")>1,"s","")),"Less than a day")
给出“1年5个月15天”的结果。