HomeGoogle DocsSpreadsheetHow to Use the DATEDIFF Function in Google Sheets QUERY

How to Use the DATEDIFF Function in Google Sheets QUERY

Looking for the regular worksheet function? Use DATEDIF (one F). Looking to calculate date differences inside a QUERY formula? Use the SQL function DATEDIFF (two Fs).

The DATEDIFF function in Google Sheets QUERY returns the number of days between two date or DateTime values. It is one of the QUERY scalar functions, meaning it returns one calculated value for each row rather than aggregating multiple rows.

Unlike the DATEDIF worksheet function, DATEDIFF always returns the difference in days and does not support units such as months or years.

In this tutorial, you’ll learn the DATEDIFF syntax, see examples, and understand how it differs from the DATEDIF worksheet function.

Syntax

DATEDIFF(end_date, start_date)
  • end_date – The later date or DateTime.
  • start_date – The earlier date or DateTime.

You can use this QUERY scalar function with Date or DateTime values.

The function returns whole numbers because it truncates the time portion before calculating the difference.

Example

I have a few work start dates in column A and work end dates in column B.

Let’s see how to use the DATEDIFF function in Google Sheets QUERY to return the number of days between the work start and end dates in each row.

=QUERY(A1:B, "Select A, B, dateDiff(B, A)")

The third column in the result contains the number of days between the two dates for each row.

Example of the DATEDIFF function in a Google Sheets QUERY formula

If you only need the number of days, use:

=QUERY(A1:B, "Select dateDiff(B, A)")

To assign a custom heading to the calculated column, use the LABEL clause.

=QUERY(A1:B, "Select A, B, dateDiff(B, A) label dateDiff(B, A) ' No. Days'")

DATEDIFF (QUERY) vs. DATEDIF (Worksheet Function)

DATEDIF is a stand-alone worksheet function, whereas DATEDIFF is a scalar function used inside a QUERY statement.

The DATEDIF worksheet function provides several units such as years, months, and days. Here we’ll compare only the behavior when calculating the difference in days.

If you use the worksheet function in cell D2, the formula would be:

=ArrayFormula(IF(A2:A*B2:B, DATEDIF(A2:A, B2:B, "D"),))

Since this is an array formula, wrapping DATEDIF in an IF statement prevents unintended results for blank rows by calculating the difference only when both dates are present.

The syntax is:

DATEDIF(start_date, end_date, unit)

Unlike DATEDIFF, the start date comes first in the DATEDIF syntax. The "D" unit returns the difference in days.

Besides the syntax, the two functions also behave differently in certain situations.

ScenarioDATEDIFDATEDIFF
Both cells empty0Empty
Start date > End date#NUM!Negative value
Start date emptyTreats the blank cell as 30-Dec-1899Empty
End date empty#NUM!Empty

Notes

  • DATEDIFF is available only inside QUERY.
  • It returns whole days only.
  • The time portion of DateTime values is ignored.
  • Blank dates return blank results instead of errors.
  • The order of the arguments is end date first, unlike DATEDIF.

Conclusion

The DATEDIFF function is useful when you need to calculate the number of days directly inside a QUERY statement. If you need years, months, or more flexible interval calculations outside QUERY, use the DATEDIF worksheet function instead.

Prashanth K V
Prashanth K V
Your Trusted Google Sheets and Excel Expert Prashanth K V is a Diamond Product Expert in Google Sheets, officially recognized by Google for his contributions to the Docs Editors Help Community and featured in the Google Product Experts Directory. Explore his blog to learn advanced formulas, automation tips, and problem-solving techniques to elevate your spreadsheet skills.

Most Discussed

More like this

Google Sheets Document Expiry Tracker Template (Free Download)

Missing your visa renewal, vehicle insurance renewal, emission certificate expiry, domain renewal, or scheduled...

Google Sheets Habit Tracker Template (Free & Automated)

Want to monitor your habits and track your progress over a month or across...

How to Sort a Pivot Table by Value in Google Sheets

Google Sheets lets you sort Pivot Tables by row fields, column fields, or summarized...

13 COMMENTS

  1. This is more of a general question relating to DATEDIF.

    I am calculating years, months, and days using the DATEDIF formula based on the total number of days.

    Here is the formula with total days being in cell A1.

    =DATEDIF(0,A1,"y")&" years – "&DATEDIF(0,A1,"ym")&" months – "&DATEDIF(0,A1,"md")&" days"

    My question: Months in the DATEDIF formula are considered full or complete months. Does this mean in my case where there are only days to calculate and no start date or end date, that complete months would mean the average number of days in a calendar month which is something like 30.437.days?

    I’ve looked all over the web to find an answer and can’t see one.

    • Hi, John,

      Please analyze the below formulas (both are equal).

      Formula 1:

      =datedif(0,64,"D")

      Formula 2:

      =datedif(date(1899,12,30),date(1899,12,30)+64,"D")

      Try replacing “D” with “MD” in both the formulas. I hope that clarifies.

  2. Hi Prashanth,

    I have 4 columns:

    Location | Tested | DateOfTest | EmployeeType
    District Office | Yes | 10/01/2020 | Staff
    Maintenance | Yes | 10/10/2020 | Staff

    I took your Query, modified it, and put it in F2 or the E column is blank:

    =ArrayFormula(QUERY({A2:D,{(DATE(YEAR(TODAY()),MONTH(TODAY()),DAY(TODAY())))}},"SELECT Col1,Col3,Col4 WHERE Col4 is not null AND datediff(Col3,Col5)<=14"))

    I'm trying to pull out the rows where Col3 <= 14 days from today. I'm getting a REF error. Any help would be appreciated.

    • Hi, Ted Stapenhorst,

      Try this Query;

      =ArrayFormula(QUERY({A2:D},"SELECT Col1,Col3,Col4 WHERE Col4 is not null and Col3<=date '"&TEXT(today()-14,"yyyy-mm-dd")&"'"))

      or the following Filter.

      =filter({A2:A,C2:D},C2:C<=today()-14)

    • Only two values like the text “Ended” in the highlighted cells and the text “Pending” in the cells which are blank (not highlighted).

      On my shared example Sheet, I have used the following formula in cell C3.

      =ArrayFormula(if($C$1:$AG$1>=AH3,"Ended",))

      which then copied to C5 and C7.

  3. Ok… this is my sample file…

    – File copied and link removed by Admin –

    I’ve edited one row manually to show you what I am trying to do.

    Thanks

    Nessus

  4. Hello there,

    I have a question about this.

    I have a sheet in sort of calendar format but the dates are as headers in one row on the top, the dates of the week are below in another row and the data are entered in the third-row below.

    Now, I want to highlight/format the cells in the row containing the data based on the date that will be specified in a separate cell that is in the same row at the end of it.

    Basically it’s a planner.

    Thanks

    Nessus

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.