Query by Date
This page shows multiple ways to query documents based on a date field.
Before Reading
This page assumes that you've already imported the Prismic library and retrieved the Client object. If you want to learn how to do this, then refer to the How to Query the API page.
Here we use a few predicates that can query based on Date or Timestamp fields. Feel free to explore the Date & Time based Predicate Reference page to learn more about this.
Here is an example of how to query for all the articles with the release_date field (Date) equal to January 22, 2017. Note that this only works for the Date Field, not the Time Stamp field.
client.query(
Prismic.Predicates.at('my.article.release_date', new Date('2017-01-22'))
).then(response => {
// res is the response object, res.results holds the documents
})
Here is an example of a query for all documents of the type "blog_post" whose release_date field (Date) is in the month of May in the year 2016. This might be useful for a blog archive.
client.query([
Prismic.Predicates.month('my.blog_post.release_date', 'May'),
Prismic.Predicates.year('my.blog_post.release_date', 2016)
]).then(res => {
// res is the response object, res.results holds the documents
})
You can also query document by their publications dates.
Here is an example of a query for all documents of the type "blog_post" whose original publication date is in the month of May in the year 2016.
client.query([
Prismic.Predicates.at('document.type', 'blog_post'),
Prismic.Predicates.month('document.first_publication_date', 'May'),
Prismic.Predicates.year('document.first_publication_date', 2016)
]).then(response => {
// res is the response object, res.results holds the documents
})