Convert date to timestamp for storing into firebase firestore in javascript(将日期转换为时间戳,以便在 javascript 中存储到 firebase firestore)
问题描述
我目前正在使用 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 的 静态 Timestamp 类 中的 静态方法.
.fromDate
is a static method from the static Timestamp class from 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将日期转换为时间戳,以便在 javascript 中存储到 firebase firestore


基础教程推荐
- Vue 3 – <过渡>渲染不能动画的非元素根节点 2022-01-01
- Electron 将 Node.js 和 Chromium 上下文结合起来意味着 2022-01-01
- Chart.js 在线性图表上拖动点 2022-01-01
- 自定义 XMLHttpRequest.prototype.open 2022-01-01
- 如何使用JIT在顺风css中使用布局变体? 2022-01-01
- 我可以在浏览器中与Babel一起使用ES模块,而不捆绑我的代码吗? 2022-01-01
- 用于 Twitter 小部件宽度的 HTML/CSS 2022-01-01
- 直接将值设置为滑块 2022-01-01
- html表格如何通过更改悬停边框来突出显示列? 2022-01-01
- 如何使用TypeScrip将固定承诺数组中的项设置为可选 2022-01-01