用角度和firebase存储日期 [英] Storing dates with angular and firebase

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

问题描述

我正在创建一个角度+火力点食品杂志,并且我正在对日期进行逻辑冻结,以及如何最好地存储它们。我的数据结构如下所示:

  {
uid:{
profile:{
name:name
},
journal:{
date1:{
breakfast:,
午餐:,
dinner:

date2:{
...
}
}




$ b显然,我只想设置一个'date'值并能够设置并从任何时区检索它。我初始设置日期键为四舍五入的时间戳,小时,分钟,秒都设置为0.但是,这并没有工作时区问题的B / C。


然后,我决定使用dateObject.toDateString()作为唯一的日期键。这工作得很好,直到我意识到这样做会限制我如何查询Firebase。

我希望能够查询Firebase一段时间以及维护订单。例如,假设我想查看2015年1月21日星期三到2015年1月29日星期四的条目。获取那些日子的数据将需要一个单独的查询和firebase请求。如果我存储了一个数字,我可以使用firebase的范围查询; startAt(),endAt()和equalTo()。

我现在的解决方案/想法是存储一个由数字对象的4位数字组成的8位数字, 2位数月份和2位数字日期,其中2015年1月21日将存储为20150021,2015年1月29日作为20150029存储。然后解析数字客户端以创建本地日期对象。



但是,好像我要么过度看待某些东西,要么过度思考。有没有更好的方法来实现这一目标? >格式是相当普遍的。它的优点是所产生的数值在数值上是可比较的,即明天的数字比今天的数字要大。

更常见的选择是存储在 (毫秒)。不仅在数值上是可比较的,而且它也是 Date.now()返回并可以传入 new Date()

I'm creating an angular + firebase food journal and I'm having a logic freeze concerning dates and how best to store them. My data structure looks like:

{
    "uid": {
        "profile": {
            "name": "name"
        },
        "journal": {
            "date1": {
                "breakfast": "",
                "lunch": "",
                "dinner": ""
            },
            "date2": {
                ...
            }
        }
    }
}

Obviously, I only want to set a 'date' value once and be able to set it and retrieve it from any timezone. I initialy set the 'date' key to a rounded off timestamp with hours, minutes, seconds all set to 0. But that didn't work b/c of the timezone issue.

I then decided to just use dateObject.toDateString() as the unique 'date' key. That worked fine until I realized that doing so would limit how I can query firebase.

I want to be able to query firebase for a range of days as well as maintain order. For example, say I want to view the entries for "Wed Jan 21 2015" to "Thu Jan 29 2015". Getting the data for those days would require a separate query and firebase request. Where as, if I stored a number, I could use firebase's range queries; startAt(), endAt(), and equalTo().

My current solution/thought is to store an 8 digit number made up of a date object's 4 digit year, 2 digit month and 2 digit day where "Wed Jan 21 2015" would be stored as 20150021 and "Thu Jan 29 2015" as 20150029. And then just parse the numbers client side to create local date objects.

But it seems like I'm either over-looking something or over-thinking something. Is there a better way to accomplish this?

解决方案

Storing dates in yyyyMMdd format is fairly common. It has the advantage that the resulting values are numerically comparable, i.e. the number for tomorrow is bigger than the one for today.

An even more common alternative is to store in "(milli)seconds since the epoch". Not only is that numerically comparable, but it is also what Date.now() returns and can be passed into new Date().

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

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