首页 > 解决方案 > 如何在 Angular Firestore 中将日期保存为时间戳?

问题描述

firestore.Timestamp.now();

我试过这种方式,它将日期保存为地图。

nanoseconds: 388000000
seconds: 1570897877

纳秒和秒类型是数字。

我想使用角度将日期保存为 Firestore 时间戳。我发现没有接受答案的重复问题。

标签: angulartypescriptgoogle-cloud-firestoretimestampangularfire2

解决方案


如果您想在 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()});

推荐阅读