js-joda
js-joda copied to clipboard
timestamp query with LocalDate
In the database, date is saved by timestamp type. Currently, this project runs by js-soda and TypeORM. I want to check the date which is applied to LocalDate.now().minusDays(1) by using createQueryBuilder like right below.
async getLastVisitCount() {
const yesterDay = LocalDate.now().minusDays(1);
return await this.createQueryBuilder('user')
.where('user.last_check_in = :date', {
date: yesterDay,
})
.getCount();
}
However, it occurs error when I perform. If I print yesterDay of variable to console.log, LocalDate { _year: 2021, _month: 11, _day: 15 } is printed. It occurs error when I form query on the right above of query, so I want to change it to timestamp type. How can I change?
Hi there. I haven't used TypeORM but have you tried converting the LocalDate to string?
where('user.last_check_in = :date', {
date: yesterDay.toString(),
})
The .toString() will convert the date to an ISO string that should be then correctly interpreted by the DB.
Does that work?