Firebase 日期存储出错 [英] Firebase Date storage goes wrong

查看:22
本文介绍了Firebase 日期存储出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想像这样在 Firebase 中存储一个日期:

So I am trying to store a date in Firebase like this:

 var fb = new Firebase(FIREBASE_URL);
        var syncData = $firebase(fb);
        var date = new Date(2014, 0, 1);
        console.log(date);
        syncData.$child('date').$set(date);
        var dateInFirebase = syncData.$child('date');
        dateInFirebase.$on('loaded', function(){
            console.log(dateInFirebase.$value);
        });

第一个日期正确记录了Wed Jan 01 2014 00:00:00 GMT+0100 (CET)",但是第二个日志是这样的:2013-12-31T23:00:00.000Z",即 1 天前那,这是firebase中的一些错误还是我遗漏了一些明显的东西?我没有发现任何其他问题,所以我倾向于认为我做错了什么,我只是不知道是什么.

The first date correctly logs 'Wed Jan 01 2014 00:00:00 GMT+0100 (CET)', however the second log is this: '2013-12-31T23:00:00.000Z' which is 1 day before that, is this some bug in firebase or am I missing something obvious? I haven't found any other questions on this so I'm inclined to think I did something wrong, I just don't know what.

好的,现在我完全糊涂了,如果我用空的构造函数(今天的日期)替换 dateconstructor,他会正确地存储日期..

Okay now I'm totally confused, if I replace the dateconstructor with the empty constructor (today's date) he stores the date correctly..

推荐答案

使用 getTime().当您读回它们并希望向用户显示时,使用 new Date(/* getTime value here */),它们将在任何时区正确显示.此外,客户端时钟可能会出现偏差,因此您应该使用 Firebase.ServerValue.TIMESTAMP 而不是 new日期().

Store dates using getTime(). When your read them back and want to display to users, use new Date(/* getTime value here */), and they will display correctly in any time zone. Additionally, client clocks may be skewed, so you should use Firebase.ServerValue.TIMESTAMP instead of new Date().

这篇关于Firebase 日期存储出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆