简体   繁体   中英

Dart/Flutter: Firebase Timestamp to Date behaves different on different devices

I have a flutter app where I am storing activities with an activity date in my firebase.

Firebase 中的活动日期

When showing those activities in my app I noticed that the Date gets converted differently from different devices.

我使用 toDate() 来转换时间戳

The output in my Virtual Device when debugging is in the end the 29.07.2018 instead of the 30.07.2018. How can I fix this?

转换后从流打印日期

在此处输入图片说明

This may happen because of locale. Try to use DateFormat from https://pub.dev/packages/intl

Firestore Timestamps are always stored in UTC, like pretty much all other date types in any modern operating system. What you see in the Firebase console is the date formatted for the locale configured on the local machine that's viewing the console.

If you want to format a timestamp in your app, you should look into using the date formatting utilities provided by Flutter or whatever platform you're using. Usually users expect to see timestamp formatted for their own chosen locale.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM