Firestore Timestamp.fromDate不是UTC [英] Firestore Timestamp.fromDate not UTC
问题描述
有人知道如何在Firestore中保留UTC时间戳吗?
Anyone knows how to persist UTC Timestamp in Firestore?
在我的 Angular
应用程序中,如果我将今天的日期转换为如下所示的时间戳,最后得到 UTC + 2
(现在是夏令时,瑞士)在Firestore数据库中的日期
In my Angular
app, if I convert today's date to a Timestamp like the following and I end up with a UTC+2
(it's summer time here right now in Switzerland) date in the Firestore database
import {firebase} from '@firebase/app';
import '@firebase/firestore';
const myTimestamp = firebase.firestore.Timestamp.fromDate(new Date());
例如,如果我尝试从冬季转换日期,那么我最终在数据库中使用 UTC + 1
时间戳
If for example I try to convert a date from the winter time, I end up with UTC+1
Timestamp in the database
const myTimestamp = firebase.firestore.Timestamp.fromDate(new Date(2019, 0, 1));
如果我要使用 now()
,我最终也会得到 UTC + 2
日期
If I would use now()
I end up with UTC+2
dates too
const now: firebase.firestore.Timestamp = firebase.firestore.Timestamp.now();
当我保留数据时,我没有做任何特别的事情:
I don't do anything particular when I persist the data:
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);
任何想法如何为Firestore创建有效的 UTC
时间戳?
Any idea how is it possible to create valid UTC
Timestamp for Firestore?
推荐答案
Firestore时间戳记中没有编码时区.它仅使用秒和纳秒(这是Timestamp对象上的字段)的组合来存储距Unix纪元的偏移量.大多数日期/时间对象都是这样的-它们不在乎timezome是什么,这只是一个绝对的时间点.
Firestore timestamps don't have a timezone encoded into them. It just stores the offset from the Unix epoch using a combination of seconds and nanoseconds (which are fields on the Timestamp object). Most date/time objects are like this - they don't care what the timezome is, it's just an absolute point in time.
如果您在控制台中查看时间戳记字段,则会从本地设置中看到计算机使用的本地时区中显示的时间.
If you view a timestamp field in the console, you will see the time displayed in the local timezone that your computer uses from its local settings.
如果将时间戳转换为JavaScript Date对象,则该日期对象自然会在浏览器的本地timezome中呈现自己,类似于控制台.
If you convert a timestamp to a JavaScript Date object, that date object naturally renders itself in the browser's local timezome, similar to the console.
如果要渲染特定时区的Date对象,则应使用一个库来为您完成此操作,例如 moment.js .
If you want to render a Date object for a specific timezone, you should use a library to do that for you, such as moment.js.
这篇关于Firestore Timestamp.fromDate不是UTC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!