In my Firebase cloud functions, I am using admin.firestore.FieldValue.serverTimestamp(),
to write a server timestamp to Firestore. The format that this outputs is October 5, 2022 at 10:42:09 PM UTC-5
. However, the problem is that when I write to Firestore from the client, I use DateTime.now().millisecondsSinceEpoch.toString(),
which outputs as 1665027682
. I don’t want to use 2 different formats for my datetimes, so how do I convert the server timestamp in cloud functions to the unix format (to match the client’s output)?
2
Answers
As mentioned by @Lalit Fauzdar you may use this code below to convert the server timestamp:
Once you have a
Timestamp
object you can get the milliseconds-since-the-epoch value by callingtoMillis()
on it. From its documentation: