Query by Date / Timestamp

Prismic no longer recommends the gatsby-source-prismic-graphql plugin

With recent changes to Gatsby, Prismic no longer recommends the gatsby-source-prismic-graphql plugin that this documentation uses. Read more about the future of Prismic and Gatsby. We highly recommend using the gatsby-source-prismic instead. The documentation for this plugin can be found in the plugin's github README.

We will leave this documentation here for now, but will change it in the future when we determine the best approach for Prismic & Gatsby.

This page shows multiple ways to query documents based on a Date field, Timestamp 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 and specify the date you are looking for with the following format: YYYY-MM-DD as shown in these examples.

Query for an exact date

The example below shows how to query all the documents of the type Article with the "event" Date field equal to October 22, 2020. Then we retrieve the same Date field that has the API ID of "event":

Copy
query ArticlesDateQuery {
  prismic {
    allArticles(where: {event: "2020-10-22"}) {
      edges {
        node {
          event
        }
      }
    }
  }
}

Query after a certain date

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

Here is an example of a query for all the documents with the type Article whose "event" Date field is after October 22, 2020. Then we retrieve the same Date field that has the API ID of "event":

Copy
query ArticlesDateQuery {
  prismic {
    allArticles(where: {event_after: "2020-10-22"}) {
      edges {
        node {
          event
        }
      }
    }
  }
}

Query before a certain date

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

Here is an example of a query for all the documents of the type Article whose "event" Date field is before October 22, 2020. Then we retrieve the same Date field that has the API ID of "event":

Copy
query ArticlesDateQuery {
  prismic {
    allArticles(where: {event_before: "2020-10-22"}) {
      edges {
        node {
          event
        }
      }
    }
  }
}

Query between two dates

To query between two dates, you just need to append the Date fields API ID with _before and _after.

Here is an example of a query for all the documents of the type Article whose "event" Date fields are after May 1, 2020 and before October 23, 2020. Then we retrieve the same Date field that has the API ID of "event":

Copy
query ArticlesDateQuery {
  prismic {
    allArticles(where: {event_after: "2020-05-01", event_before: "2020-10-23"}) {
      edges {
        node {
          event
        }
      }
    }
  }
}

Query by a Timestamp field

When querying by a Timestamp field, you will need to use the where argument and and specify the date you are looking for with the following format: YYYY-MM-DDTHH:MM:SS+0000 as shown in these examples.

Query for an exact date & time

The example below shows how to query all the documents of the type Article with the "appointment" Timestamp field equal to October 22, 2020 at 10pm GMT. Then we retrieve the same Timestamp field that has the API ID of "appointment":

Copy
query ArticlesTimestampQuery {
  prismic {
    allArticles(where: {appointment: "2020-10-22T22:00:00+0000"}) {
      edges {
        node {
          appointment
        }
      }
    }
  }
}

Query after a certain date & time

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 documents of the type Article whose "appointment" Timestamp field is after October 22, 2020 at 10pm GMT. Then we retrieve the same Timestamp field that has the API ID of "appointment":

Copy
query ArticlesTimestampQuery {
  prismic {
    allArticles(where: {appointment_after: "2020-10-22T22:00:00+0000"}) {
      edges {
        node {
          appointment
        }
      }
    }
  }
}

Query before a certain Timestamp field

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 documents of the type Article whose "appointment" Timestamp field is before October 22, 2020 at 10pm GMT. Then we retrieve the same Timestamp field that has the API ID of "appointment":

Copy
query ArticlesTimestampQuery {
  prismic {
    allArticles(where: {appointment_before: "2020-10-22T22:00:00+0000"}) {
      edges {
        node {
          appointment
        }
      }
    }
  }
}

Query between Timestamp fields

To query between a given date, you just need to append the Timestamp fields API ID with _before and _after.

Here is an example of a query for all the documents of the type Article whose "appointment" Timestamp fields are after May 01, 2020 at 10pm GMT and before October 23, 2020 at 10pm GMT. Then we retrieve the same Timestamp field that has the API ID of "appointment":

Copy
query ArticlesTimestampQuery {
  prismic {
    allArticles(where: {appointment_after: "2020-05-01T22:00:00+0000", appointment_before: "2020-10-23T22:00:00+0000"}) {
      edges {
        node {
          appointment
        }
      }
    }
  }
}

Query by a publication date

When querying by a publication date, you will need to use the where argument and specify the date, time, & timezone you are looking for as shown in these examples.

Query for an exact publication timestamp

You can use the firstPublicationDate and lastPublicationDate arguments to query an exact given date and time.

Note that this query is likely never to be used as you would need to specify the exact moment that the article was published. The before, after, and between queries shown below are likely more interesting for you.

This example below shows how to query all the documents of the type Article with the first publication date on October 22, 2020 at 10pm GMT. Then we retrieve the meta data with the firstPublicationDate field:

Copy
query ArticlesQuery {
  prismic {
    allArticles(firstPublicationDate: "2020-10-22T22:00:00+0000") {
      edges {
        node {
          _meta {
            firstPublicationDate
          }
        }
      }
    }
  }
}

This example below shows how to query all the documents of the type Article with the last publication date in October 22, 2020 at 10pm GMT. Then we retrieve the meta data with the lastPublicationDate field:

Copy
query ArticlesQuery {
  prismic {
    allArticles(lastPublicationDate: "2020-10-22T22:00:00+0000") {
      edges {
        node {
          _meta {
            lastPublicationDate
          }
        }
      }
    }
  }
}

Query after a certain publication 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 the documents of the type Article with the first publication date after October 22, 2020 at 10pm GMT. Then we retrieve the meta data with the firstPublicationDate field:

Copy
query ArticlesQuery {
  prismic {
    allArticles(firstPublicationDate_after: "2020-10-22T22:00:00+0000") {
      edges {
        node {
          _meta {
            firstPublicationDate
          }
        }
      }
    }
  }
}

Here is an example that shows how to query all the documents of the type Article with the last publication date after October 22, 2020 at 10pm GMT. Then we retrieve the meta data with the lastPublicationDate field:

Copy
query ArticlesQuery {
  prismic {
    allArticles(lastPublicationDate_after: "2020-10-22T22:00:00+0000") {
      edges {
        node {
          _meta {
            lastPublicationDate
          }
        }
      }
    }
  }
}

Query before a certain publication 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 the documents of the type Article with the first publication date before October 22, 2020 at 10pm GMT. Then we retrieve the meta data with the firstPublicationDate field:

Copy
query ArticlesQuery {
  prismic {
    allArticles(firstPublicationDate_before: "2020-10-22T22:00:00+0000") {
      edges {
        node {
          _meta {
            firstPublicationDate
          }
        }
      }
    }
  }
}

Here is an example that shows how to query all the documents of the type Article with the last publication date before October 22, 2020 at 10pm GMT. Then we retrieve the meta data with the lastPublicationDate field:

Copy
query ArticlesQuery {
  prismic {
    allArticles(lastPublicationDate_before: "2020-10-22T22:00:00+0000") {
      edges {
        node {
          _meta {
            lastPublicationDate
          }
        }
      }
    }
  }
}

Query between certain publication timestamps

For this, you can use the firstPublicationDate_before/ firstPublicationDate_after and lastPublicationDate_before/ lastPublicationDate_after arguments.

Here is an example of a query for all the documents of the type Article with the first publication date after March 01, 2020 at 10pm GMT and the first publication date before October 22, 2020 at 10pm GMT. Then we retrieve the meta data with the firstPublicationDate:

Copy
query ArticlesQuery {
  prismic {
    allArticles(firstPublicationDate_after: "2020-03-01T22:00:00+0000", firstPublicationDate_before: "2020-10-22T22:00:00+0000") {
      edges {
        node {
          _meta {
            firstPublicationDate
          }
        }
      }
    }
  }
}

Here is an example of a query for all the documents of the type Article with the last publication date after March 01, 2020 at 10pm GMT and the last publication date before October 22, 2020 at 10pm GMT. Then we retrieve the meta data with the lastPublicationDate:

Copy
query ArticlesQuery {
  prismic {
    allArticles(lastPublicationDate_after: "2020-03-01T22:00:00+0000", lastPublicationDate_before: "2020-10-22T22:00:00+0000") {
      edges {
        node {
          _meta {
            lastPublicationDate
          }
        }
      }
    }
  }
}