Dav*_*sco 1 javascript datetime angular google-cloud-firestore
任何人都知道如何在 Firestore 中保留 UTC 时间戳?
在我的Angular应用程序中,如果我将今天的日期转换为如下所示的时间戳,并且最终UTC+2在 Firestore 数据库中得到一个(现在是瑞士的夏令时)日期
import {firebase} from '@firebase/app';
import '@firebase/firestore';
const myTimestamp = firebase.firestore.Timestamp.fromDate(new Date());
Run Code Online (Sandbox Code Playgroud)
例如,如果我尝试从冬季时间转换日期,我最终会UTC+1在数据库中使用Timestamp
const myTimestamp = firebase.firestore.Timestamp.fromDate(new Date(2019, 0, 1));
Run Code Online (Sandbox Code Playgroud)
如果我使用,我也会now()得到UTC+2日期
const now: firebase.firestore.Timestamp = firebase.firestore.Timestamp.now();
Run Code Online (Sandbox Code Playgroud)
当我保留数据时,我没有做任何特别的事情:
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);
Run Code Online (Sandbox Code Playgroud)
知道如何UTC为 Firestore创建有效的时间戳吗?
Dou*_*son 10
Firestore 时间戳没有编码到其中的时区。它只是使用秒和纳秒(它们是 Timestamp 对象上的字段)的组合来存储与 Unix 纪元的偏移量。大多数日期/时间对象都是这样的——它们不在乎时区是什么,它只是一个绝对的时间点。
如果您在控制台中查看时间戳字段,您将看到以本地时区显示的时间,您的计算机在其本地设置中使用该时区。
如果将时间戳转换为 JavaScript 日期对象,则该日期对象自然会在浏览器的本地时区中呈现自身,类似于控制台。
如果您想为特定时区呈现 Date 对象,您应该使用一个库来为您执行此操作,例如moment.js。
| 归档时间: |
|
| 查看次数: |
6632 次 |
| 最近记录: |