将日期转换为时间戳以在JavaScript中存储到Firebase Firestore中 [英] Convert date to timestamp for storing into firebase firestore in javascript
问题描述
我当前正在使用 Math.floor(Date.now()/ 1000)
来获取正确的时间戳格式,以将其添加到Firebase中的文档中,但是,时间戳记是作为数字而不是作为时间戳记插入的。
I'm currently using Math.floor(Date.now() / 1000)
to get the correct timestamp format to add into a document in Firebase, however, the timestamp gets inserted as a number, and not as a timestamp.
我希望按如下所示插入它(作为时间戳,而不是数字)。
I would like to have it inserted as shown below (as a timestamp, not as a number).
可能吗?
推荐答案
您可以简单地使用以下行:
You can simply use the following line:
var myTimestamp = firebase.firestore.Timestamp.fromDate(new Date());
.fromDate
是静态方法来自 Firebase 的静态时间戳类。
引用: Firebase时间戳文档
更新:
对于云函数,请查看JGuo的评论:
For cloud functions look at JGuo's comment:
如果您正在编写云函数,则将变为
admin.firestore.Timestamp.fromDate()
– JGuo 1月21日1:56
If you are writing cloud functions, it becomes
admin.firestore.Timestamp.fromDate()
– JGuo Jan 21 at 1:56
这篇关于将日期转换为时间戳以在JavaScript中存储到Firebase Firestore中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!