有人知道如何在 Firestore 中保留 UTC 时间戳吗?
In my Angular
应用程序,如果我将今天的日期转换为如下所示的时间戳,我最终会得到一个UTC+2
(瑞士现在是夏令时)Firestore 数据库中的日期
import {firebase} from '@firebase/app';
import '@firebase/firestore';
const myTimestamp = firebase.firestore.Timestamp.fromDate(new Date());
例如,如果我尝试转换冬季时间的日期,我最终会得到UTC+1
数据库中的时间戳
const myTimestamp = firebase.firestore.Timestamp.fromDate(new Date(2019, 0, 1));
如果我会用now()
我最终得到UTC+2
也有日期
const now: firebase.firestore.Timestamp = firebase.firestore.Timestamp.now();
当我保留数据时,我没有做任何特别的事情:
const now: firebase.firestore.Timestamp = firebase.firestore.Timestamp.now();
const myTimestamp = firebase.firestore.Timestamp.fromDate(new Date());
const myData = {
created_at: now,
another_value: myTimestamp
};
await this.collection.add(myData);
知道如何创建有效的UTC
Firestore 的时间戳?
Firestore 时间戳没有编码到其中的时区。它只是使用秒和纳秒(Timestamp 对象上的字段)的组合来存储距 Unix 纪元的偏移量。大多数日期/时间对象都是这样的 - 它们不关心时区是什么,它只是一个绝对时间点。
如果您在控制台中查看时间戳字段,您将看到计算机在其本地设置中使用的本地时区中显示的时间。
如果将时间戳转换为 JavaScript Date 对象,该日期对象自然会在浏览器的本地时区中呈现自身,类似于控制台。
如果您想呈现特定时区的 Date 对象,您应该使用库来为您做到这一点,例如moment.js https://momentjs.com/.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)