3

I have a table in my supabase database with two columns start end end. Both are of type timestampz. Now I want to query all entries where the current timestamp (Date.now()) is inbetween start and end.

I thought I could do something like this (TypeScript):

var currentTimestamp = Date.now();
await serviceSupbaseClient
        .from("table")
        .select(
          "id, name, start, end"
        )
        .gte("start", currentTimestamp)
        .lte("end", currentTimestamp)
        .single()

But this does not work. I also tried formatting my currentTimestamp in the same String iso format that I get from the select.

Is this something that is not possible to do with postgrest supabase? Or what am I doing wrong?

Edit: This is my entry in my supabase db (start/end has different name.): enter image description here

and my current Code is this:

var currentTimestamp = Date();
console.log(currentTimestamp);
await serviceSupbaseClient
    .from("lobby")
    .select(
      "id, name, valid_from, valid_to"
    )
    .lte("valid_from", currentTimestamp)
    .gte("valid_to", currentTimestamp)
    .single()

I get this error:

'invalid input syntax for type timestamp with time zone: "Fri Jan 26 2024 12:35:28 GMT+0000 (Coordina'

when using var currentTimestamp = Date.now();

I get this error: date/time field value out of range: "1706272571131"

2
  • You say "But this does not work", but what exactly happens? Does it return the wrong data? Does it return an error? In your example, you are querying data that are greater than or equal to the current time stamp, and also less than or equal to the current timestamp. Such data most likely does not exist.
    – dshukertjr
    Jan 17 at 7:37
  • @dshukertjr It returns nothing. I compare different column values to my current time stamp. The data exists.
    – progNewbie
    Jan 17 at 10:31

1 Answer 1

2

You have the start time and end time in the wrong place. Your start time needs to be earlier than the current timestamp, and the end time needs to be later than the current timestamp. Flip your lte and gte, and you should see some data.

var currentTimestamp = new Date().toISOString();
const {data, error} = await serviceSupbaseClient
        .from("table")
        .select(
          "id, name, start, end"
        )
        .lte("start", currentTimestamp)
        .gte("end", currentTimestamp)
        .single()
9
  • Actually this does not change a thing in my case. I always get no results back.
    – progNewbie
    Jan 23 at 15:11
  • @progNewbie I have just edited my answer right now. Try converting the Date object to toIso8601String. If this does not work, I will need to see some sample data that you have which you think will be queried using the query you shared.
    – dshukertjr
    Jan 23 at 23:55
  • In what package is toIso8601String included? (I am in Typescript Edge function) And isn't this converting it to local time? On the Supabase DB it is UTC per default.
    – progNewbie
    Jan 25 at 15:40
  • @progNewbie My bad, I thought it was Dart. I have edited my answer to a Typescript format.
    – dshukertjr
    Jan 25 at 23:39
  • Thanks! Sadly this doesn't fix it. I just always get null in return. I edited my question with further information.
    – progNewbie
    Jan 26 at 12:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.