fluttergoogle-cloud-firestore

Storing time only in Firestore


Is there a way to store a time value only in Firestore (without date)? I am trying to achieve the following in a Flutter app (business hours):

company [document]
  hours [map]
    - mon [map]
      - open: 08:00 [timestamp...?]
      - close: 18:00
    - tue [map]
      - open: 08:00
      - close: 18:00

I'd like to store it in a format that can be used in calculations (ie, how long until company closes today).

I've tried using TimeStamp and setting a generic date for each day (01/01/2001), and then only retrieving the time using DateFormat('HH:ss'), but the problem is that it then returns a string which can't be used in calculations.

Could anyone please point me in the right direction?


Solution

  • Firestore doesn't provide a native "time of day" type. Timestamp is supposed to be used for a specific point in time, which wouldn't apply here.

    A common way to represent time of day would be hours and minutes as a four digit integer in "mmss" format. So, for example, 1330 for 1:30pm. Or if you need seconds granularity, just add those to the end: 133000. You can then sort by these numbers to filter by time of day in addition to date. (You might want to store date in "YYYYMMDD" format.)