Documentation/Calc Functions/DATEDIF

From The Document Foundation Wiki
Jump to navigation Jump to search

Function name:

DATEDIF

Category:

Date and Time

Summary:

Determines the difference between two given dates, returning the result in days, months, or years as specified in the third argument.

Syntax:

DATEDIF(Start date; End date; Interval)

Returns:

Returns a non-negative integer which is the difference between the two given dates in days, months, or years (as specified in the Interval argument).

Arguments:

Start date is the date (in quotation marks) or a date-time serial number, or a reference to the cell containing one of those types, which is the start date of the period for which the calculation is to be carried out.

End date is the date (in quotation marks) or a date-time serial number, or a reference to the cell containing one of those types, which is the end date of the period for which the calculation is to be carried out. End date must not be earlier than Start date.

Interval is a string, or a reference to a cell containing that string, which determines which result is returned. The accepted strings are listed in the following table and they are not case sensitive (so, for example, "d" and "D" are equivalent).

Interval strings Value returned by DATEDIF
"d" Number of days between the two dates.
"m" Number of months between the two dates.
"y" Number of years between the two dates.
"ym" Number of months between the two dates, ignoring years.
"md" Number of days between the two dates, ignoring years and months.
"yd" Number of days between the two dates, ignoring years.
  • If either Start date or End date is not a valid date expression, then DATEDIF reports an invalid argument error (Err:502).
  • If Start date is later than End date, then DATEDIF reports an invalid argument error (Err:502).
  • If Interval does not match one of the six expected strings, then DATEDIF reports an invalid argument error (Err:502).
  • DATEDIF ignores any time components of Start date or End date.

Additional details:

General information about Calc's Date and Time functions

Note pin.svg

For convenience the information in this subsection is repeated on all pages that describe functions in the Date and Time category.


  • Internal to Calc, a date/time value is handled as a real number and this is sometimes referred to by the term date-time serial number. For example, 2021-02-08 12:00:00 corresponds to the date-time serial number 44235.5. The integer part of a date-time serial number (before the decimal point) is the number of days since a defined start date. The fractional part of a date-time serial number (after the decimal point) is the time of day expressed as a fraction of a day. For example, 0.25 is equivalent to 06:00:00 and 0.75 is equivalent to 18:00:00.
  • Calc uses 1899-12-30 as the default start date (day 0) but you can go to Tools > Options > LibreOffice Calc > Calculate to select alternative start dates; however, unless you have specific requirements for compatibility with legacy spreadsheet applications, we recommend that you do not change this setting. If you copy and paste cells containing date values between different spreadsheets, check that both documents are set to use the same start date to avoid unexpected changes to the dates displayed.
  • Negative date-time serial numbers correspond to dates earlier than Calc's start date. Calc calculates all dates back to 15th October 1582 using the Gregorian calendar. The day before this is 4th October 1582, and Calc uses the Julian calendar for earlier dates.
  • A date-time serial number is the same as any other number, except that we usually choose to display it formatted as a date/time. You can enter a number in a cell and then change the cell’s formatting to display the value as a date/time. You can also enter a date/time in a cell and then change the cell’s formatting to display the date-time serial number.
  • For clarity, the descriptions of the Calc functions within these wiki pages use the standard ISO 8601 format for date and time because this should be independent of your selected locale settings. The default date format on your computer may be different. When entering dates as part of formulas, slashes or dashes used as date separators may be 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 either use the DATE function or place the date in quotation marks and use the ISO 8601 notation. Avoid using locale dependent date formats which may produce errors if the document is loaded under different locale settings.
  • Time zone information is not used by Calc’s Date & Time functions.
  • If you only wish to enter two digits of the year (for example 21 instead of 2021), Calc needs to know whether you mean 2021 or 1921. Go to Tools > Options > LibreOffice > General to view or adjust the range of years which Calc will use to interpret two-digit year values.
  • Calc contains logic to decide how to treat text when encountered as an operand in an arithmetic operation or as an argument to a function that expects a number instead. In this context, unambiguous conversion is possible for ISO 8601 dates and times in their extended formats with separators. For example, the simple formula =1 + "2021-02-08" will usually give the result 2021-02-09 (when formatted as a date). However, this logic may be affected by the settings on the Detailed Calculation Settings dialog, accessed by selecting Tools > Options > LibreOffice Calc > Formula and pressing the Details button in the Detailed Calculation Settings area. If a #VALUE! error occurs, it may be because the Generate #VALUE! error option is selected, in which case you should experiment with one of the other self-explanatory options in the Conversion from text to number drop-down.

Examples:

In the following examples, it is assumed that Philippe was born on 1974-04-17 (date-time serial number 25674) and that today's date is 2012-06-13 (date-time serial number 39611).

Formula Description Returns
=DATEDIF("1974-04-17"; "2012-06-13"; "y") Here the function calculates the difference between the two dates in whole years, showing that Philippe is 38 years old. 38
=DATEDIF(DATE(1974,4,17); "2012-06-13"; "m") Here the function calculates the difference between the two dates in whole months, showing that Philippe has been alive for 457 months 457
=DATEDIF("1974-04-17"; 39611; "d") Here the function calculates the difference between the two dates in days, showing that Philippe has been alive for 13937 days. 13937
=DATEDIF("1974-04-17"; "2012-06-13"; "ym") Here the function calculates the difference between the two dates in whole months but ignoring years, showing that it is 1 month since Philippe's 38th birthday. 1
=DATEDIF("1974-04-17"; "2012-06-13"; "md") Here the function calculates the difference between the two dates in days but ignoring years and months. Using the result from the previous example, this shows that Philippe is 38 years, 1 month, and 27 days old. 27
=DATEDIF("1974-04-17"; "2012-06-13"; "yd") Here the function calculates the difference between the two dates in days but ignoring years, showing that Philippe's birthday was 57 days ago. 57

Related LibreOffice functions:

DAYS

DAYS360

ODF standard:

Section 6.10.3, part 2

Equivalent Excel functions:

DATEDIF