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

查看:112
本文介绍了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);
        });

第一个日期正确记录为"2014年1月1日星期三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..

推荐答案

使用 Firebase.ServerValue.TIMESTAMP 而不是新的Date().

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天全站免登陆