What is the recommended way to query a dateTime fi...
# orm-help
s
What is the recommended way to query a dateTime field when you want posts created/edited on a particular date?
βœ… 1
a
Hey πŸ‘‹πŸΎ One suggestion would be using the
gte
and
lte
filters to query
DateTime
values. Assuming you have the following schema:
Copy code
model Post {
  id        String   @id @default(cuid())
  title     String
  createdAt DateTime @default(now())
}
You could filter the post’s
createdAt
field as follows:
Copy code
await prisma.post.findMany({
  where: {
    createdAt: {
      gte: new Date(/** range start */).toISOString(),
      lte: new Date(/** range end */).toISOString(),
    }
  }
})
πŸ‘ 1
s
I guess if I have a date like June 14, I will need to create a June 13 and a Jun 15 to make the filtering work.
a
Yes. I would recommend specifying the year as well to narrow down your search, for example:
Copy code
new Date('2020 June 14').toISOString()
is:
'2020-06-13T21:00:00.000Z'
πŸ‘ 1
s
Thanks Alex. Love Prisma, but hoping there's more in-depth date filtering methods in the future like the other ORMs.
πŸ’― 1
πŸ‘πŸΎ 1
Will use what you suggested for now.
a
Are there any specific date filtering methods you think might be lacking in Prisma? Happy to hear your thoughts and help us improve the existing DX πŸ™‚
s
Coming from Django, there are more robust methods like these that offer more control and are less code. https://docs.djangoproject.com/en/4.0/ref/models/querysets/#date
a
If you don’t mind, could you create a feature request – stating the example filters we could provide when working with DateTime values? πŸ™‚
s
I think I already have, let me check. If I haven't I will now. Thanks for all the help. Appreciate it. πŸ™‚
πŸ™ŒπŸΎ 1