angular - 如何在 Angular Firestore 中将日期保存为时间戳?
问题描述
firestore.Timestamp.now();
我试过这种方式,它将日期保存为地图。
nanoseconds: 388000000
seconds: 1570897877
纳秒和秒类型是数字。
我想使用角度将日期保存为 Firestore 时间戳。我发现没有接受答案的重复问题。
解决方案
如果您想在 Firestore 文档中添加当前时间作为时间戳值,您可以使用FieldValue.serverTimestamp()
Doug 提到的方法添加它,如下例所示:
import * as firebase from 'firebase';
[ . . . ]
db.collection('times').add({time: firebase.firestore.FieldValue.serverTimestamp()});
如果您想获取特定时间,可以创建一个新的 Date 对象并使用其toDateString()
上的函数将其添加到 Firestore,如下所示:
import * as firebase from 'firebase';
[ . . . ]
date = new Date('2018-11-16T09:48:51.137Z'); //sample date
this.db.collection('times').add({spec_time: this.date.toDateString()});
推荐阅读
- azure - 使用 ARM-TTK 测试 ARM 模板
- javascript - DataTable 更新到 1.11 销毁返回错误
- pandas - Groupby agg保持空白值
- github - 该应用程序需要以下 .net 框架版本之一。如何为github解决这个问题?
- java - 如何使用 io.reactivex.rxjava3.core
- php - Wordpress如何在转义html标签时使用换行符
- python - 过滤日志级别
- javascript - 我尝试在松露中部署我的智能合约,但是当我“松露迁移”时说“不支持的编译器”我该如何解决这个问题?
- r - 如何将 kableExtra 表格添加到 RMarkdown Powerpoint 幻灯片中
- kubernetes - 对主机 https://gitlab.host.com 的 SSH 密钥扫描失败,错误:dial tcp:lookup tcp///gitlab.host.com:getaddrinfow: The specified class was not foun