question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

find operators like MoreThan and LessThan doesn't work properly for date fields

See original GitHub issue

Issue type:

[ ] question [x] bug report [ ] feature request [ ] documentation issue

Database system/driver:

[ ] cordova [ ] mongodb [ ] mssql [ ] mysql / mariadb [ ] oracle [ ] postgres [x] sqlite [ ] sqljs [ ] react-native

TypeORM version:

[x] latest [ ] @next [ ] 0.x.x (or put your version here)

Steps to reproduce or a small repository showing the problem: find operators like MoreThan and LessThan doesn’t work properly for date fields. some investigation showed that sqlite stores dates like 2018-06-05 15:57:27.249 but the query is like ... WHERE "User"."createdAt" > ? -- PARAMETERS: ["2018-06-05T01:01:32.650Z"] meaning it queries in ISO date format. when you just need comparison till day factor, it doesnt look like a problem but when you also need the time/hours/mins/etc then it doesnt return proper results. i only tested for sqlite so i dont know if same problem goes for other dbs.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:30
  • Comments:13 (2 by maintainers)

github_iconTop GitHub Comments

25reactions
bioleylcommented, Jan 20, 2019

@enif-lee Ok thanks. I usually use an other one: https://www.npmjs.com/package/date-fns You just don’t use the parameter date and you force new Date() in your function. Here is an update if someone is interested:

import { LessThan, MoreThan } from 'typeorm'
import { format } from 'date-fns'

// TypeORM Query Operators
export const MoreThanDate = (date: Date) => MoreThan(format(date, 'YYYY-MM-DD HH:MM:SS'))
export const LessThanDate = (date: Date) => LessThan(format(date, 'YYYY-MM-DD HH:MM:SS'))
// ...
const user = await repository.findOne({
    createdAt: MoreThanDate(new Date())
})
19reactions
ospfrancocommented, Mar 12, 2020

I was doing unit testing and found this problem, how is this still open for so long… very basic functionallity for an ORM.

I have a postgres db with timestamp fields in my entity, this is what I ended up using to make it work:

import { format } from 'date-fns';

export function ISOToDBDate(isoDate: string) {
  return format(new Date(isoDate), 'yyyy-MM-dd kk:mm:ss.SSS');
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Date criteria doesn't work in my query - Microsoft Support
All dates. The criteria is missing the "And" operator, so it doesn't filter any results out. Dates on or after 1/1/05 and on...
Read more >
How to Compare Dates in Excel (Greater/Less Than ...
This can be done using a simple equal-to-operator. The above formula would return TRUE if the compared dates are the same, and FALSE...
Read more >
Logical operators in Excel: equal to, not equal to, greater than ...
The tutorial explains the basics of Excel logical operators such as equal to, not equal to, greater than or equal to, less than...
Read more >
Mysql date function not working for less than - Stack Overflow
It is sargable because the column is compared to a constant expression (the result of the + operation being deterministic), and therefore an...
Read more >
Dates & Comparison Operators in Excel's IF Function - YouTube
Download the feature file here. https://excels-if- function.com/2020/0... This video shows you how to correctly use dates as criteria in ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found