简体   繁体   English

Firebase 时间戳返回错误日期 - Javascript

[英]Firebase Timestamp returns with wrong date - Javascript

I'm trying to get the Timestamp value from firestore (using Firebase Functions), and I´ve successfully done it localy with the toDate() method of Timestamp, and moment library.我正在尝试从 firestore 获取时间戳值(使用 Firebase 函数),并且我已经使用 Timestamp 的 toDate() 方法和矩库成功地在本地完成了它。

moment(doc.data().EndDate.toDate())

But when I deploy my code to firebase and test the function, somehow the toDate() returns a Date with 1 less hour than the saved timestamp on firebase.但是当我将我的代码部署到 firebase 并测试 function 时,toDate() 以某种方式返回一个比 ZBF12E1515C25C7D8C0352F1413AB9A1 上保存的时间戳少 1 小时的日期I suppose it is transforming my date to UTC, since I'm in UTC+1, and the Timestamp is also stored with UTC+1 in firestore, but I don't know how to reliably get the timestamp date as is in firestore, regardless of timezones.我想它正在将我的日期转换为UTC,因为我在UTC + 1,并且时间戳也与UTC + 1一起存储在firestore中,但我不知道如何可靠地获取firestore中的时间戳日期,无论时区如何。

If someone knows why this happens or has any idea how to solve it it would be great.如果有人知道为什么会发生这种情况或知道如何解决它,那就太好了。

All timestamps in Firestore are stored in UTC. Firestore 中的所有时间戳都以 UTC 格式存储。 If you see something different in the Firebase console, that's just your browser formatting it for your local timezone.如果您在 Firebase 控制台中看到不同的内容,那只是您的浏览器将其格式化为您的本地时区。

In JavaScript, all Date objects are also represented in UTC.在 JavaScript 中,所有 Date 对象也以 UTC 表示。 If you format that as a string, you will again possibly get a different representation based on your local timezone.如果您将其格式化为字符串,您可能会再次根据您的本地时区获得不同的表示形式。

If you write code that computes values using dates or timestamps, you should perform all your computations using UTC .如果您编写使用日期或时间戳计算值的代码,则应使用 UTC 执行所有计算 This is the pretty much all computing systems want to deal with dates.这几乎是所有计算系统想要处理的日期。 When it comes time to format the date for display to an end user, only then should you take timezone into account, and present something according to the user's preferences.当需要格式化日期以显示给最终用户时,您才应该考虑时区,并根据用户的喜好呈现一些东西。

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

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