skip to Main Content

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


  1. As mentioned by @Lalit Fauzdar you may use this code below to convert the server timestamp:

    Math.floor((new Date).getTime()/1000);
    
    Login or Signup to reply.
  2. Once you have a Timestamp object you can get the milliseconds-since-the-epoch value by calling toMillis() on it. From its documentation:

    The point in time corresponding to this timestamp, represented as the number of milliseconds since Unix epoch 1970-01-01T00:00:00Z.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search