以年、日、时、分格式表示的两个日期之间的差异

Difference between two dates in years days hours minutes format

我试图找出两天“xx 年 xx 天 xx 小时 xx 分钟”格式之间的区别。我的开始日期在 A 列中,结束日期在 B 列中。计算 C 列中的差异。

我在 C 列中使用 B1-A1 公式并在 C 列中使用自定义格式 "yy" years "dd" days "hh" hours "mm" minutes"。如果开始年份和结束年份不同,它就可以正常工作。但如果相同,则 C 列报告不正确的年份和日期。 (显示为 99 年 30 天)。

如何解决?

Sample google sheet link

如果两个值都相同则放入if条件否则应该做减法

=IF(A1=B1,"0",B1-A1)

这是因为 GS 将零日期视为其日历的第一天,即 1899-12-30。您可以使用以下公式得到正确的结果:

       =TEXT(DATEDIF(A2,B2,"Y"),"00 \y\ear\s ") & 
         TEXT(TRUNC(EDATE(B2,-12*(DATEDIF(A2,B2,"Y")))-A2), "00 \da\y\s ") & 
         TEXT(ABS((B2-TRUNC(B2,0))-(A2-TRUNC(A2,0))+1),"hh \hour\s mm \minut\e\s")

或使用数组公式:

    =ArrayFormula(IF(LEN(A2:A),
     TEXT(DATEDIF(A2:A,B2:B,"Y"),"00 \y\ear\s ") & 
     TEXT(TRUNC(EDATE(B2:B,-12*(DATEDIF(A2:A,B2:B,"Y")))-A2:A), "00 \da\y\s ") & 
     TEXT(ABS((B2:B-TRUNC(B2:B,0))-(A2:A-TRUNC(A2:A,0))+1),"hh \hour\s mm \minut\e\s"),))

该公式考虑了闰年并计算经过的天数。

有点长,但它有效。想尝试 TEXT,但我对这些天有额外的条件,因为它很棘手,因为工作表如何计算差异,而实际上它们每个月有不同的天数。您需要考虑每个月的天数,因为它是独一无二的。

公式:

=datedif(A2, B2, "y")&" years "
&if(day(B2) >= day(A2),
if(index(split(B2, " "), ,2) >= index(split(A2, " "), ,2),
day(B2) - day(A2),
day(B2) - day(A2) - 1),
if(index(split(B2, " "), ,2) >= index(split(A2, " "), ,2),
day(eomonth(B2, -1)) + day(B2) - min(day(A2), day(eomonth(B2, -1))),
day(eomonth(B2, -1)) + day(B2) - min(day(A2), day(eomonth(B2, -1))) -1))&" days "
&hour(B2 - A2)&" hours "
&minute(B2 - A2)&" minutes"

输出:

注:

  • 公式负责格式化。
  • 由于每个月的天数不同,天数的公式非常复杂。但这应该显示更准确的答案
  • index(split(B2, " "), ,2) >= index(split(A2, " "), ,2) 添加到原始 以考虑开始日期的较晚时间,如果发生这种情况,将天数减少 1

参考:

您的 B2 - A2 公式实际上得到了正确的结果 — 它是一个日期序列值,以 .[=18 为单位表示两个时刻之间的差异=]

问题出在您使用的自定义数字格式上。它指的是 calendar 时间,而您需要 elapsed 时间。不幸的是,Google Sheets 显然只支持以小时、分钟和秒为单位的经过时间。

如果文本字符串结果对您来说没问题,请使用:

=datedif(A2, B2, "y") & " years " & datedif(A2, B2, "d") - 365 * datedif(A2, B2, "y") & " days " & text(B2 - A2 - int(B2 - A2), "HH ""hours"" mm ""minutes""")

该公式假设所有年份都是 365 天,当日期跨度跨越闰日时,该公式将不正确。

要获得平均运行时间,使用数组公式计算所有行的 =B2 - A2,然后计算这些结果的平均值,最后根据您的喜好格式化结果,如下所示:

=arrayformula( average( if( B2:B + A2:A, B2:B - A2:A, iferror(1/0) ) ) )

=datedif(0, G2, "y") & " years " & datedif(0, G2, "d") - 365 * datedif(0, G2, "y") & " days " & text(G2 - 0 - int(G2 - 0), "H ""hours"" m ""minutes""")

查看 sample spreadsheet 中的单元格 F2:G2

有关日期和时间值在电子表格中的工作原理的说明,请参阅 this answer

尝试:

=INDEX(IFERROR(TRIM(REGEXREPLACE(
 TEXT(                DATEDIF(A2:A, B2:B, "Y"),   "0#")&CHOOSE(MATCH(                DATEDIF(A2:A, B2:B, "Y"),   {0,1,2})," years ", " year ", " years ")&
 TEXT(IF(B2:B-A2:A<1,,DATEDIF(A2:A, B2:B, "YD")), "0#")&CHOOSE(MATCH(IF(B2:B-A2:A<1,,DATEDIF(A2:A, B2:B, "YD")), {0,1,2})," days ",  " day ",  " days ")&
 TEXT(    TEXT(B2:B-A2:A, "H"),    "0#")&               CHOOSE(MATCH(TEXT(B2:B-A2:A, "H")  *1,                   {0,1,2})," hours ", " hour ", " hours ")&
 TEXT(INT(TEXT(B2:B-A2:A, "M.S")), "0#")&               CHOOSE(MATCH(TEXT(B2:B-A2:A, "M.S")*1,                   {0,1,2})," minutes"," minute"," minutes"),
 "\b0 (year(s)|day(s)|hour(s)|minute(s))", ))))
  • 适用于闰年
  • 适用于单数
  • 适用于复数
  • 适用于数组
  • 适用于秒数
  • 仅适用于日期
  • 仅适用于时间
  • 工作不到一天
  • 适用于空行
  • 排除所有空值


仅删除前导空值:

=INDEX(IFERROR(TRIM(REGEXREPLACE(
 TEXT(                DATEDIF(A2:A, B2:B, "Y"),   "00")&CHOOSE(MATCH(                DATEDIF(A2:A, B2:B, "Y"),   {0,1,2})," years ", " year ", " years ")&
 TEXT(IF(B2:B-A2:A<1,,DATEDIF(A2:A, B2:B, "YD")), "00")&CHOOSE(MATCH(IF(B2:B-A2:A<1,,DATEDIF(A2:A, B2:B, "YD")), {0,1,2})," days ",  " day ",  " days ")&
 TEXT(    TEXT(B2:B-A2:A, "H"),                   "00")&CHOOSE(MATCH(TEXT(B2:B-A2:A, "H")  *1,                   {0,1,2})," hours ", " hour ", " hours ")&
 TEXT(INT(TEXT(B2:B-A2:A, "M.S")),                "00")&CHOOSE(MATCH(TEXT(B2:B-A2:A, "M.S")*1,                   {0,1,2})," minutes"," minute"," minutes"),
 "^\b(?:00 years )?(?:00 days )?(?:00 hours )?(?:00 minutes)?", ))))


NOT删除任何空值:

=INDEX(IFERROR(REGEXREPLACE(
 TEXT(                DATEDIF(A2:A, B2:B, "Y"),   "00")&CHOOSE(MATCH(                DATEDIF(A2:A, B2:B, "Y"),   {0,1,2})," years ", " year ", " years ")&
 TEXT(IF(B2:B-A2:A<1,,DATEDIF(A2:A, B2:B, "YD")), "00")&CHOOSE(MATCH(IF(B2:B-A2:A<1,,DATEDIF(A2:A, B2:B, "YD")), {0,1,2})," days ",  " day ",  " days ")&
 TEXT(    TEXT(B2:B-A2:A, "H"),                   "00")&CHOOSE(MATCH(TEXT(B2:B-A2:A, "H")  *1,                   {0,1,2})," hours ", " hour ", " hours ")&
 TEXT(INT(TEXT(B2:B-A2:A, "M.S")),                "00")&CHOOSE(MATCH(TEXT(B2:B-A2:A, "M.S")*1,                   {0,1,2})," minutes"," minute"," minutes"),
 "^\b00 years 00 days 00 hours 00 minutes", )))


让它与火星、木星和土星对齐:

=INDEX(IFERROR(REGEXREPLACE(REGEXREPLACE(
 TEXT(                DATEDIF(A2:A, B2:B, "Y"),    "00")&CHOOSE(MATCH(                DATEDIF(A2:A, B2:B, "Y"),   {0,1,2})," years ", " year  ", " years ")&
 TEXT(IF(B2:B-A2:A<1,,DATEDIF(A2:A, B2:B, "YD")), "000")&CHOOSE(MATCH(IF(B2:B-A2:A<1,,DATEDIF(A2:A, B2:B, "YD")), {0,1,2})," days ",  " day  ",  " days ")&
 TEXT(    TEXT(B2:B-A2:A, "H"),                    "00")&CHOOSE(MATCH(TEXT(B2:B-A2:A, "H")  *1,                   {0,1,2})," hours ", " hour  ", " hours ")&
 TEXT(INT(TEXT(B2:B-A2:A, "M.S")),                 "00")&CHOOSE(MATCH(TEXT(B2:B-A2:A, "M.S")*1,                   {0,1,2})," minutes"," minute "," minutes"),
 "^\b00 years 000 days 00 hours 00 minutes", ), "(0)(\d{2})", " ")))



更新:

对于平均使用情况:

=INDEX(AVERAGE(IF(B2:B+A2:A, B2:B-A2:A, )))

或:

=INDEX(AVERAGE(IFERROR(1/(1/(B2:B-A2:A)))))

取决于您需要的灵敏度