js-joda icon indicating copy to clipboard operation
js-joda copied to clipboard

timestamp query with LocalDate

Open zaehuun opened this issue 4 years ago • 1 comments

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?

zaehuun avatar Nov 16 '21 14:11 zaehuun

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?

InExtremaRes avatar Nov 18 '21 23:11 InExtremaRes