What is the best way to store date data?

Continuing the discussion from Query items of a collection before a certain date?:

In this example about dates, the data is stored in “Time” type.
I’m not sure I want to do this.

Just like in this example, I need to access documents between two dates.
Do dates have to be in “Time” for this to be done properly?

For example, if I keep the dates as “Epoch”, can I still access documents between Range and two dates?

@hasimyerlikaya You should be able to use Epoch dates and Range.

Data:
“plannedDate”: “2021-12-14T00:00:00.000”,

Range:
[“2021-01-01”], [“2021-08-30”]

I store the date as a String and query it as a String again in the Range.

It seems to be working, will there be any problem with that?

AFAIK, it should not be a problem. I want to call out that you cannot use Time and Date functions on this field.

1 Like

The reason that you cannot use the Time and Date functions on your timestamp string is that the timezone is missing. The library used for parsing timestamp strings is rather fussy about timezones.

If your timestamps included the timezone, such as “2021-12-14T00:00:00.000Z”, then the Time and Date functions would work just fine:

> Time("2021-12-14T00:00:00.000")
Error: invalid argument
{
  errors: [
    {
      position: [],
      code: 'invalid argument',
      description: "Cannot cast '2021-12-14T00:00:00.000' to a time."
    }
  ]
}
> Time("2021-12-14T00:00:00.000Z")
Time("2021-12-14T00:00:00Z")
> ToDate(Time("2021-12-14T00:00:00.000Z"))
Date("2021-12-14")

Actually, I didn’t need time information.
What I wanted to do was keep the data as a String and query it with Range and again String.

This also seems to work, thank you.