Dates on query filter not workign

Hi CodeCrew,

Need help! I think I’m just missing a line or a step for my query to work with a date filter… My app runs a query and get documents in firestore… Each document comes with a date (timestamp) field… The latest date is recorded so next time the query runs, it won’t retrieve the old documents or the documents app already retrieves.

In Firestore database, the document date field is “Sept 22, 2023 at 12:06:29 at UTC+10” (As shown in the Firestore console)

Query and code below:


print("Before - lastNotificationDate=\(self.lastNotificationDate)")

let query = dbNotifications
            .whereField("userID", isEqualTo: userID)
            .whereField("notifDate", isGreaterThan: self.lastNotificationDate!)
            .order(by: "notifDate", descending: false)

               query.getDocuments() { snapShot, error in
                       .....
                       ..... 
                       // Get notificaiton date..
                        if let notifTempStamp = doc.get("notifDate") as? Timestamp {
                            userNotification.notifDate = notifTempStamp.dateValue()
                            
                            print("notifDate=\(userNotification.notifDate)")
                        }

                        self.lastNotificationDate = userNotification.notifDate

The print on the above code shows - “notifDate=2023-09-22 02:06:29 +0000”

When the query run again, the print on the last date recorded is:

“Before - lastNotificationDate=Optional(2023-09-22 02:06:29 +0000)”

Whats not working and what I need help from are:

  • Every time the query is re-run, it still retrieves the document. Is there anything I need to do or some conversion needed to happen before passing the “lastNotificationDate” into the query filter?
  • As shown above, the date printed (notifDate) is not the equivalent AEST (Australian Eastern Standard Time). My mac is set to Australian Time (AEST). What timezone is the date when I call notifTempStamp.dateValue()? If its not AEST, how do I change it?

Stuck with this so any help will be much appreciated. Thanks…

Dates are tricky in Firebase.

This stackoverflow thread might be helpful.

Thanks Chris,

Went through the link you gave and research some more but still don’t know why it doesn’t work. For some reason though, my app is suddenly working now… Will revisit this later.

What I have found out though is that the date in firestore is the date/timezone from my PC which is already in AEST. On the simulator or debug though dates are in UTC.

Will dig some more on this later… Will let you know if I still need help on this…

Thanks again…

Cheers,