1

I have stored in Firestore documents with createdAt field containing date and timezone of the user. For example(December 22, 2019 at 9:21:42 PM UTC-3)

Then, I have a cloud functions where I want to retrieve all of Today documents. The problem is that the cloud function doesn't know the Timezone, and that gets messy when retrieveng documents of diferent timezones.

const now: Date = new Date();

const docQuery = await db
   .collection('users')
   .where('createdAt', '>', new Date(now.getFullYear(), now.getMonth(), now.getDate()))
   .where('createdAt', '<', new Date(now.getFullYear(), now.getMonth(), now.getDate(), 23, 59, 59))
   .get();

The now date is in UTC-0 and if I have a document from the day before at 22:00 UTC-3, the query retrieves it, but technically it is not a Today document.

Any idea on how to tackle this issue?

Sebastian
  • 3,666
  • 2
  • 19
  • 32
  • 1
    You will have to initialize the Date object with the specific timezone you want. Or, you will have to use some other library to help with that. – Doug Stevenson Dec 23 '19 at 16:27
  • I understand, so a proper solution would be that the frontend send a parameter to de cloud functions with the required timezone of the device and use that to create the Date for the query, is that correct? Thanks @DougStevenson – Sebastian Dec 23 '19 at 18:09
  • 1
    Yes, you will need to get a timezone (or the specific date time in milliseconds to convert to a date) from whoever defines what a "day" is. – Doug Stevenson Dec 23 '19 at 18:14

1 Answers1

0

Use timestamp inside the firestore documents instead of date.

ref.doc(key).set({
  created : firebase.firestore.Timestamp.fromDate(new Date("December 10, 1815 19:00:00 EST"))
}) 

A Timestamp represents a point in time independent of any time zone or calendar, represented as seconds and fractions of seconds at nanosecond resolution in UTC Epoch time.

You can then use the method toDate() to convert the timestamp to a Date object.

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
  • And how could I do it if the dateTime was selected by the user? – Sebastian Dec 23 '19 at 15:49
  • Check this https://cloud.google.com/firestore/docs/manage-data/add-data – Peter Haddad Dec 23 '19 at 16:04
  • `var docData = { stringExample: "Hello world!", booleanExample: true, numberExample: 3.14159265, dateExample: firebase.firestore.Timestamp.fromDate(new Date("December 10, 1815")), arrayExample: [5, true, "hello"], nullExample: null, objectExample: { a: 5, b: { nested: "foo" } } }; db.collection("data").doc("one").set(docData).then(function() { console.log("Document successfully written!"); });` – Peter Haddad Dec 23 '19 at 16:04
  • So doing `firebase.firestore.Timestamp.fromDate(new Date("December 10, 1815")),` will ignore the date timezone? – Sebastian Dec 23 '19 at 16:07
  • Firestore server timestamps are not going to help when it comes to querying. They are only used for setting a field value to the current moment in time. The query needs to provide the precise moment in time for the query, either through a Date object, or a Timestamp object. – Doug Stevenson Dec 23 '19 at 16:27
  • @DougStevenson doesn't this give a timestamp object `var docData = { stringExample: "Hello world!", booleanExample: true, numberExample: 3.14159265, dateExample: firebase.firestore.Timestamp.fromDate(new Date("December 10, 1815")), arrayExample: [5, true, "hello"], nullExample: null, objectExample: { a: 5, b: { nested: "foo" } } }; db.collection("data").doc("one").set(docData).then(function() { console.log("Document successfully written!"); })` – Peter Haddad Dec 23 '19 at 16:33
  • @DougStevenson `firebase.firestore.Timestamp.fromDate(new Date("December 10, 1815"))` – Peter Haddad Dec 23 '19 at 16:33
  • The code in your answer is showing a server timestamp for setting a field. Your comment here is showing something very different. – Doug Stevenson Dec 23 '19 at 16:34
  • But when I read ur comment first(before you edit the comment) I thought we can't use timestamp for querying in firestore, that's why i undeleted the answer to ask you @DougStevenson – Peter Haddad Dec 23 '19 at 16:36
  • You can use a timestamp, but the code you're showing now still doesn't provide a timezone for the date, which is what the OP is asking about. The marked duplicate here explains how to do that. – Doug Stevenson Dec 23 '19 at 16:38