Save timestamp in Firebase with React Studio


#1

I am storing values in a collection in Firebase and I would like to add a field with a timestamp so I can query by that. How could I add the timestamp with React Studio?


#2

Add the timestamp to input object with Script in “Save data” interaction. In the example I’ve done it with Unix timestamp.


#3

I have been busy creating an admin panel to manage the database so I can edit all elements in the database and add new elements. I used a modification of your suggested script and it works fine but there a few things I am not entirely convinced.

I believe some of the issues I am experiencing is that when I use the script, the date is stored as a “number”, which is the long number that correspond to the seconds. But as Firestore has a specific way to point the date with the “Timestamp” field, I believe it would be more appropriate to use that “Timestamp” field. How could I modify the script to add a “Timestamp” that is understood but the Firestore database?


#4

I’m not an expert on this but if you want to store the server timestamp you could write a cloud function which adda server timestamp every time when new document is created.

For ”client timestamp” I would still save time as unix time to prevent any possible issues with different time zones etc.


#5

Thank you as always for your answer. It would be fine for me to use Unix time. The thing I am having some issues and I thought it was because I was not using the Timestamp option. But I have made some tests and there is something wrong in my script or somewhere. When I create a new document, I have the following script to save the date:


However, I just saved a new document and the saved date is wrong. When I went to check the number in a page where it converts the Unix time to a date this is the result:

I believe it is saving a longer number that it should and that is why it is not working properly.

What did I do wrong?


#6

Hi, is it possible that the website your’e using is converting seconds not milliseconds? If you look at the getTime() Javascript specs it returns “milliseconds from 1970” not seconds.


#7

That’s right. And I also discover that I had some inconsistencies in the way the date was stored in my database. For some documents the date was a string and for others it was a number. I just fixed all of them and made all the date fields to be a number in milliseconds. When I add a new document it now works perfectly.

Thank you!