[英]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.在我的 Firebase 云函数中,我使用
admin.firestore.FieldValue.serverTimestamp(),
将服务器时间戳写入 Firestore。 The format that this outputs is October 5, 2022 at 10:42:09 PM UTC-5
.此输出的格式为
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
.但是,问题是当我从客户端写入 Firestore 时,我使用
DateTime.now().millisecondsSinceEpoch.toString(),
输出为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 种不同的格式,那么如何将云函数中的服务器时间戳转换为 unix 格式(以匹配客户端的输出)?
Once you have a Timestamp
object you can get the milliseconds-since-the-epoch value by calling toMillis()
on it.获得
Timestamp
object 后,您可以通过调用toMillis()
来获取自纪元以来的毫秒值。 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.
此时间戳对应的时间点,表示为自 Unix 纪元 1970-01-01T00:00:00Z 以来的毫秒数。
As mentioned by @Lalit Fauzdar you may use this code below to convert the server timestamp:正如@Lalit Fauzdar 所提到的,您可以使用下面的代码来转换服务器时间戳:
Math.floor((new Date).getTime()/1000);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.