Query by date

This page shows multiple ways to query documents based on a Date field or by a document's first or last publication date.


Query by a Date field

When querying by a Date field, you will need to use the where argument as shown in these examples.

Query for an exact date

To query for an exact date, you just need to specify the date you are looking for with the following format: YYYY-MM-DD. The example below shows how to query all the article documents with the "release_date" field equal to January 22, 2017.

Copy
query{
  allArticles(where:{release_date:"2017-01-22"}){
    edges{
      node{
        title
        release_date
      }
    }
  }
}

Query after a certain date

To query after a given date, you just need to append the field API ID with _after.

Here is an example of a query for all the articles whose "release_date" field is after January 22, 2017.

Copy
query{
  allArticles(where:{release_date_after:"2017-01-22"}){
    edges{
      node{
        title
        release_date
      }
    }
  }
}

Query before a certain date

To query before a given date, you just need to append the field API ID with _before.

Here is an example of a query for all the articles whose "release_date" field is before January 22, 2017.

Copy
query{
  allArticles(where:{release_date_before:"2017-07-22"}){
    edges{
      node{
        title
        release_date
      }
    }
  }
}

Query by a Timestamp field

When querying by a Timestamp field, you will need to use the where argument as shown in these examples.

Query after a certain timestamp

To query after a given date and time, you just need to append the Timestamp field API ID with _after.

Here is an example of a query for all the events whose "event_date" field is after January 22, 2017 at 5pm GMT.

Copy
query{
  allEvents(where:{event_date_after:"2017-01-22T17:00:00+0000"}){
    edges{
      node{
        title
        event_date
      }
    }
  }
}

Query before a certain timestamp

To query before a given date and time, you just need to append the Timestamp field API ID with _before.

Here is an example of a query for all the events whose "event_date" field is before January 22, 2017 at 5pm GMT.

Copy
query{
  allEvents(where:{event_date_before:"2017-01-22T17:00:00+0000"}){
    edges{
      node{
        title
        event_date
      }
    }
  }
}

Query by a publication date

There are specific query arguments provided to query by the first or last publication dates.

Query after a certain timestamp

You can use the firstPublicationDate_after and lastPublicationDate_after arguments to query after a given date and time.

Here is an example that shows how to query all "article" documents with the first publication date after January 22, 2017 at 8AM GMT.

Copy
query{
  allArticles(firstPublicationDate_after:"2017-01-22T08:00:00Z"){
    edges{
      node{
        title
        _meta{
          firstPublicationDate
        }
      }
    }
  }
}

Here is an example that shows how to query all "blog_post" documents with the last publication date after January 22, 2017 at 8AM GMT.

Copy
query{
  allBlog_posts(lastPublicationDate_after:"2017-01-22T08:00:00Z"){
    edges{
      node{
        title
        _meta{
          lastPublicationDate
        }
      }
    }
  }
}

Query before a certain timestamp

You can use the firstPublicationDate_before and lastPublicationDate_before arguments to query before a given date and time.

Here is an example that shows how to query all "page" documents with the first publication date before January 22, 2017 at 8AM GMT.

Copy
query{
  allPages(firstPublicationDate_before:"2017-01-22T08:00:00Z"){
    edges{
      node{
        title
        _meta{
          firstPublicationDate
        }
      }
    }
  }
}

Here is an example that shows how to query all "event" documents with the last publication date before January 22, 2017 at 8AM GMT.

Copy
query{
  allEvents(lastPublicationDate_before:"2017-01-22T08:00:00Z"){
    edges{
      node{
        title
        _meta{
          lastPublicationDate
        }
      }
    }
  }
}