简体   繁体   English

如何使用 Moment 在 React-Native 中将 Firestore 时间戳 UTC 转换为 UTC+1?

[英]How to convert Firestore Timestamp UTC to UTC+1 in React-Native using Moment?

I want to use MomentJs to convert Firestore Timestamp (which is by Default UTC) to UTC+1.我想使用 MomentJs 将 Firestore 时间戳(默认为 UTC)转换为 UTC+1。 How can I do that?我怎样才能做到这一点?

firestore.Timestamp.now().toDate()

PS I dont want to use the local/device's time. PS我不想使用本地/设备的时间。

Firestore Timestamp (which is by Default UTC) Firestore 时间戳(默认为 UTC)

There is no "default" really.真的没有“默认”。 When you have a Timestamp object (or a JavaScript Date , or any other native date object from a modern operating system), the moment in time is always represented internally in UTC.当您有一个 Timestamp 对象(或JavaScript Date或来自现代操作系统的任何其他本机日期对象)时,时间总是在内部以 UTC 表示。

When you call toDate() on a timestamp, the resulting Date is still internally represented in UTC.当您在时间戳上调用toDate()时,生成的日期仍以 UTC 内部表示。

Don't be confused about the "default" string representation of a Date object.不要对 Date 对象的“默认”字符串表示形式感到困惑。 It will appear in the local timezone where the computer's operation system is configured.它将出现在配置计算机操作系统的本地时区中。 But internally, it's still UTC.但在内部,它仍然是 UTC。

If you want to format the timestamp as a string for another timezone, you can certainly do that.如果您想将时间戳格式化为另一个时区的字符串,您当然可以这样做。 momentjs has an accompanying library Moment Timezone that can help you with that. momentjs 有一个随附的库Moment Timezone可以帮助您解决这个问题。 The momentjs library itself will not help you with timezones (other than the one provided by the local of the system where it is running), as it's generally understood that native timestamps are always UTC. momentjs 库本身不会帮助您处理时区(除了由运行它的本地系统提供的时区),因为通常理解本机时间戳始终是 UTC。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我想在React Native中将firebase时间戳转换为UTC timeview - I wanted to convert firebase timestamp to UTC timeview in react native 如何在 React Native 中使用 moment 将日期和时间格式转换为时间戳? - How to convert date and time format to timestamp in React Native using moment? 将当地时间转换为UTC并添加至UTC日期时间 - convert local time to utc and add to utc datetime moment 如何在 React-Native 中利用 Moment 组件 - How to capitalize a Moment component in React-Native 在 react-native 上使用 onSnapshot() 时,显示 firestore 时间戳会出错 - Displaying firestore timestamp gets error when using onSnapshot() on react-native React-Native 中的 FireStore - FireStore in React-Native 如何使用moment js将Json对象日期转换为react native? - How to convert Json object date using moment js into react native? 如何将 Firestore 与 React-native 一起使用? - How to use Firestore with React-native? 如何使用 React-Native 和 Moment.js 创建倒计时组件? - How to create countdown component with React-Native and Moment.js? 我应该如何将 firestore object 转换成我可以在 expo/react-native 中使用的东西? - How should I convert a firestore object into something I can use in expo/react-native?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM