简体   繁体   English

如何将 Firebase Cloud Functions 的时间戳格式转换为类似 1665346052 的格式?

[英]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.

相关问题 如何在 Cloud Functions for Firebase 中获取服务器时间戳? - How do I get the server timestamp in Cloud Functions for Firebase? 如何在 Firebase Cloud Functions 中对 npm 模块使用 `import`? - How do I use `import` for npm modules in Firebase Cloud Functions? 如何在 firebase 云函数中正确地将 signInWithEmailAndPassword() 转换为 createCustomToken() - Node.js - How to properly convert signInWithEmailAndPassword() to createCustomToken() in firebase cloud functions - Node js 如何从 Cloud Functions 访问和操作 Firebase 数据库? - How can I reach and manipulate Firebase Database from Cloud Functions? 如何将 Firebase 参数化配置与 Typescript 一起用于 Cloud Functions? - How can I use Firebase Parameterized configuration with Typescript for Cloud Functions? 如何避免 Firebase 云函数可能出现的竞争条件? - How do you avoid a possible race condition with firebase cloud functions? 如何在typescript函数中获取Firebase中的TImestamp类型? - How to get TImestamp type in Firebase functions in typescript? 如何将 Firestore 日期/时间戳转换为 JS Date()? - How do I convert a Firestore date/Timestamp to a JS Date()? 如何为 firebase 云功能设置 vpc 连接器? - How to setup vpc connector for firebase cloud functions? 如何使用 Firebase Cloud Messaging 安排本地推送通知? - How do I schedule a local push notification with Firebase Cloud Messaging?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM