将用户输入的日期时间转换为 firestore 时间戳值

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

我有一个用户输入的日期字符串,我想将它保存在 firestore 的时间戳字段中。 当我使用

Timestamp.fromDate(new Date('2023-03-03 18:06:00'))
时,它保存在时间戳字段中,但时间错误(3/3/23,晚上 11:36)。 当我使用
new Date('2023-03-03 18:06:00').toLocaleString('en-US', { timeZone: 'Asia/Calcutta' });
然后使用
Timestamp.fromDate()
它正在抛出一个错误。 我的输入是“2023-03-03 18:06:00” 那应该存储在 firestore 中值为 3/3/23, 06:06PM 的字段中

node.js firebase google-cloud-platform google-cloud-firestore momentjs
1个回答
0
投票

Firebase 控制台使用您计算机的时区配置来显示您当地时间的时间戳值,但该值以 UTC 格式存储在 Firestore 中。

所以你在问题中描述的是正确的情况,因为“亚洲/加尔各答”时区比 UTC 时间早 5 小时 30 分钟。您在 Firebase 控制台中看到 3/3/23, 11:36PM,因为您在 Firestore 中保存了 3/3/2023, 06:06PM。

结论:在您的应用程序中,您需要根据用户要求格式化日期,知道在 Firestore 中时间戳是 UTC。

© www.soinside.com 2019 - 2024. All rights reserved.