Ecto query considering timezone

Hi, I have a application using phoenix with Ecto to make queries on a postgres db.

My db has fields with datetime in UTC format. I want to make queries on this db with Ecto considering the local timezone.

from(u in User) |> where([u], u.created_at <= ^input_datetime) |> Repo.all

The above query take the created_at in utc format to make the comparison. I don’t want this. I want Ecto convert created_at to local timezone before the comparison to obtain the correct result.

Is there a way to do this correction?

easiest would be to convert input_datetime to utc…

1 Like

Do you know a smart way to convert the input date (ex. 2017-01-01) to correct datetime utc (ex 2017-01-01 02:00:00 )?

timex should be able to do it…

1 Like

This post solved the problem!