admin管理员组文章数量:1426597
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);
});
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.
EDIT: Okay now I'm totally confused, if I replace the dateconstructor with the empty constructor (today's date) he stores the date correctly..
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);
});
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.
EDIT: Okay now I'm totally confused, if I replace the dateconstructor with the empty constructor (today's date) he stores the date correctly..
Share Improve this question edited May 14, 2014 at 9:58 Robin-Hoodie asked May 14, 2014 at 9:08 Robin-HoodieRobin-Hoodie 4,9844 gold badges32 silver badges64 bronze badges4 Answers
Reset to default 6Store 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().
I figured out why Firebase does this, my system is on GMT+2 time and the new date(year, month, day)
constructor constructs the date at midnight. Firebase however saves the date as a UTC date so 2 hours get subtracted from the date, which leads to every date being saved at 10PM the day before the intended date.
This is also why new Date()
did work, because a date at the current hour with 2 hours subtracted still works.
A workaround for this is just setting every date at 12:00 PM instead of 12:00 AM. If anyone knows of any better workarounds, it would be appreciated.
This answer might only be relevant for languages where you have a method for this, but in C# I can do dateFromFireBase.ToLocalTime()
. This assumes that the date is saved in the same time zone as you load it in, which is the case for me. This should be possible in javascript too.
Also date store like this
var cdate = new Date(); //2016-06-14 11:34:53
var current_date = cdate.getFullYear()+"-"+cdate.getDate()+"-"+cdate.getMonth()+" "+cdate.getHours() + ":" + cdate.getMinutes() + ":" + cdate.getSeconds();
本文标签: javascriptFirebase Date storage goes wrongStack Overflow
版权声明:本文标题:javascript - Firebase Date storage goes wrong - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745482663a2660245.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论