简体   繁体   中英

How do I convert Firebase Cloud Functions' timestamp format into something like 1665346052?

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)?

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.

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

Math.floor((new Date).getTime()/1000);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM