Back

Technologies:

javascriptjavascript
htmlhtml
avatar
Tolerim
a month ago

The accuracy of timestamps in Firebase and Google Chart is in question.

The objective of this code is to display the temperature of a specific area and store it in the Firebase database. However, the implementation that records the current date into the database is faulty. It records a different time, causing the chart to display no data since it only displays data from the current day. The implemented timestamp: firebase.database.ServerValue.TIMESTAMP could not be fixed by replacing it with other methods like date.now().
Answers(2)
avatar
Vogi
a month ago
Verified Answer
It seems like you are having trouble with the timestamp implementation in Firebase. According to, Firebase.ServerValue.TIMESTAMP is not an actual timestamp but a constant that will be replaced with an actual value in the server if you have it set into some variable. You can try using admin.firestore.Timestamp.now() instead of Firebase.ServerValue.TIMESTAMP as a workaround. If you are still having trouble, you can check whether you are using the same version of Timestamp implementation.

Sources

Firebase TIMESTAMP to date and Time - Stack Overflow[Firestore] admin.firestore.Timestamp.now() fails with firebase-admin ...Can't fetch data from firebase using Timestamp - Stack OverflowTimestamp not working with firebase-functions-testTimestamp | Firebase
avatar
Tolerim
a month ago
The issue with the code recording the wrong timestamp could be due to different time zones. The new Date() constructor creates a new date object in the current time zone of the user's browser. However, the Firebase Realtime Database's ServerValue.TIMESTAMP property returns the timestamp in UTC time zone. To fix this, you can change the code where you set the timestamp to:
timestamp: firebase.database.ServerValue.TIMESTAMP
This will ensure that Firebase records the UTC timestamp, which will be the same for all users regardless of their time zone. Also, when retrieving the data from the database, make sure to convert the timestamp back to the local time zone of the user's browser. You can do this using the getTimezoneOffset() method of the Date object. Here's an example:
data.addRow([new Date(childData.timestamp - new Date().getTimezoneOffset() * 60000), childData.temperature]);
This will subtract the user's time zone offset (in minutes) from the UTC timestamp and create a new Date object with the local time zone. By doing this, the chart should display the correct temperature data for the current day in the user's local time zone.
;