如何以角度显示月/年格式的 Firestore 时间戳?

问题描述 投票:0回答:1

我正在从 firestore 集合中获取数据并显示在图表上,但是由于日期是时间戳,因此它是这样显示的。我想渲染为 MM/YYYY ,那么我该怎么做呢?

 const date = this.data.map((item) => item.weightDate); 

这是我获取要显示日期的地方。

angular typescript date google-cloud-firestore timestamp
1个回答
0
投票

正如 @Doug Stevenson 在评论中提到的,你必须将时间戳转换为 Javascript 日期对象。然后根据您的喜好格式化日期对象。

您可以使用 Timestamp.toDate() 方法将 Firestore Timestamp 转换为 Javascript Date 对象。

const date = data().weightDate.toDate();
const formattedDate = `${date.getMonth() + 1}/${date.getFullYear()}`; // Your Required formatted date.
© www.soinside.com 2019 - 2024. All rights reserved.