Module
Contents
Index 🔎︎

## DATEDIF

该函数可返回“起始日期”与“终止日期”之间的整数天数、月份数或年数。

### Syntax

DATEDIF(起始日期; 终止日期; 间隔单位)

起始日期 是开始计算的日期。

终止日期 是计算终止的日期。终止日期必须晚于起始日期。

间隔单位 为字符串，有效值有“d”“m”“y”“ym”“md”或“yd”。

When entering dates as part of formulas, slashes or dashes used as date separators are interpreted as arithmetic operators. Therefore, dates entered in this format are not recognized as dates and result in erroneous calculations. To keep dates from being interpreted as parts of formulas use the DATE function, for example, DATE(1954;7;20), or place the date in quotation marks and use the ISO 8601 notation, for example, "1954-07-20". Avoid using locale dependent date formats such as "07/20/54", the calculation may produce errors if the document is loaded under different locale settings.

Unambiguous conversion is possible for ISO 8601 dates and times in their extended formats with separators. If a #VALUE! error occurs, then unselect Generate #VALUE! error in LibreOffice - PreferencesTools - Options
- LibreOffice Calc - Formula, button Details... in section "Detailed Calculation Settings", Conversion from text to number list box.

“间隔单位”有效值 | 返回值
"d" | “起始日期”与“终止日期”之间的整数天数。
"m" | “起始日期”与“终止日期”之间的整数月份数。
"y" | “起始日期”与“终止日期”之间的整数年数。
"ym" | “起始日期”与“终止日期”之间的整数月份数，忽略年数。
"md" | “起始日期”与“终止日期”之间的整数天数，忽略年月数。
"yd" | “起始日期”与“终止日期”之间的整数天数，忽略年数。

### Example

生日计算。某人生于 1974-04-17。今天日期为 2012-06-13。

=DATEDIF("1974-04-17";"2012-06-13";"y") yields 38.

=DATEDIF("1974-04-17";"2012-06-13";"ym") yields 1.

=DATEDIF("1974-04-17";"2012-06-13";"md") yields 27.

So he is 38 years, 1 month and 27 days old.

=DATEDIF(DATE(1974,4,17);"2012-06-13";"m") yields 457, he has been living for 457 months.

=DATEDIF("1974-04-17";"2012-06-13";"d") 得到 13937，他已经活了 13937 天。

=DATEDIF("1974-04-17";DATE(2012;06;13);"yd") yields 57, his birthday was 57 days ago.