HomeGoogle DocsSpreadsheetHow to Use the Datediff Function in Google Sheets Query

How to Use the Datediff Function in Google Sheets Query

Published on

DATEDIFF is one of the scalar functions in Google Sheets Query, which returns one value per row. It doesn’t aggregate the data.

There are three types of functions and operators for data manipulations in Google Sheets Query: Aggregation functions, Scalar functions, and arithmetic operators.

The above comes in the first category.

To learn the DATEDIF worksheet function, please follow the date functions guide.

The DATEDIFF function in Query (see the double “F”) returns the date difference between two dates or timestamp values.

On the other hand, the DATEDIF is far more advanced and can return the number of days, months, years, etc., between two dates.

DATEDIFF Function in Google Sheets Query: Example

First, you can learn how to find the difference between two dates using the DATEDIFF scalar function in Google Sheets Query.

Then you can experiment with some real-life examples like;

  1. How to find the upcoming birthdays using Query?
  2. How do we extract the expiring contracts within one month, 30 days, or any specific number of days?

1. Find the Number of Days Between Two Dates in the Query

Syntax:

dateDiff(end date, start date)

We can use this Query scalar function in a date or DateTime column.

The formula will return integer values because it truncates time values before calculation.

Sample Data:

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 given work start and end dates in each row.

Formula (D1):

=QUERY(A1:B,"Select A,B,dateDiff(B,A)")
The datediff function in Query - example

You can just use =QUERY(A1:B,"Select dateDiff(B,A)") to return one column with the number of days.

DATEDIFF in Query Vs DATEDIF Worksheet Function: Differences

The DATEDIF is a stand-alone worksheet function, whereas the DATEDIFF is a scalar function for use inside Query.

DATEDIF is feature rich. We will only consider how the formulas differ in returning the difference in days between two date or DateTime values.

If I use the worksheet function in cell D1, the formula must be like this.

It’s like;

=ArrayFormula(datedif(A2:A5,B2:B5,"D"))

It’s as per the following syntax where the “D” represents days.

datedif(start date,end date,unit)

Regarding the difference with DATEDIFF, here, the start date comes first in the order.

The above difference is related to syntax, but the following one is related to calculation.

If we accidentally specify a start date greater than the end date, the scalar function will return a negative number, whereas the worksheet one will return the #NUM error.

Real-life Use of the DATEDIFF Function in Google Sheets Query

Please go through the below two examples.

1. Finding the Upcoming Birthdays Using a Query Formula

Here my sample data is like this.

Column A contains the date of birth of a few persons. Their names are in column B.

=ArrayFormula(QUERY({A2:B,{(date(year(today()),month(A2:A),day(A2:A)))}},"select Col1,Col2 where Col2<>'' and dateDiff(Col3,now())=30"))

I’ve used the above Query formula in cell D2.

extract upcoming birthdays using Query

It returns the DOBs and names whose birthdays are coming in 30 days from today’s date.

Formula Explanation

In this example, there are two columns: DOB (A) and Name (B).

We have virtually added a third column that extracts the months and days from column A and adds the current year to form a date.

For example, the DOB of the person in cell A2 is 1/30/1980.

With the below formula, we can change only the year part of this date.

=date(year(today()),month(A2),day(A2))

I’ve used the DATEDIFF function inside the Query formula to find the number of days between today’s date with this newly formed date.

datediff(Col3,now())

If the result is 30, Query selects those DOBs and Names, else it returns an #N/A error.

2. Extracting the Expiring Contracts Within One Month, 30 Days, or N Days

If you could understand the above example, solving this problem is easy.

Here is how to use the DATEDIFF function inside the Query for that.

=QUERY({A2:B},"select Col1,Col2 where Col2<>'' and dateDiff(Col1,now())=30")
extract the rows containing expiry dates

You can change the =30 to >30 or any other number to set a different expiry.

Additional Tips

Above, I have used the NOW() function to return today’s date. It’s hardcoded inside the formula.

If it’s in a cell, e.g., in cell C1, use the below formula.

=QUERY({A2:B},"select Col1,Col2 where Col2<>'' and dateDiff(Col1,date '"&TEXT(C1,"yyyy-mm-dd")&"')=30")

Related: How to Use Date Criteria in Query Function in Google Sheets.

That’s all about the use of the DATEDIFF function in Google Sheets Query.

Prashanth KV
Prashanth KV
Your Trusted Google Sheets and Excel Guide Prashanth KV brings a wealth of experience in Google Sheets and Excel, cultivated through years of work with multinational corporations in Mumbai and Dubai. As a recognized Google Product Expert in Docs Editors, Prashanth shares his expertise through insightful blogging since 2012. Explore his blog for practical tips and guidance on maximizing your spreadsheet skills.

Get Top N Values Using Excel’s FILTER Function

When using the FILTER function in Excel to extract the top N values, it's...

XLOOKUP in Excel: Working with Visible Rows in a Table

In Excel, the XLOOKUP function isn't inherently designed to exclusively work with visible rows;...

SUMPRODUCT Differences: Excel vs. Google Sheets

The core purpose of the SUMPRODUCT function remains consistent across both Excel and Google...

Sum Values by Month and Category in Excel

With the help of EOMONTH, UNIQUE, and SUMIFS functions in Excel, you can efficiently...

More like this

Interactive Random Task Assigner in Google Sheets

You have multiple tasks and multiple people. Here's a fun way to randomly assign...

Google Sheets Bar and Column Chart with Target Coloring

To color each data point in the column or bar chart according to the...

Analyzing Column A Between Non-Adjacent Values in Column B: Google Sheets

This tutorial addresses a common scenario encountered in Google Sheets: how to sum, average,...

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.