在Firebase数据库中有效地存储时间? [英] Store Time efficiently in Firebase Database?

查看:98
本文介绍了在Firebase数据库中有效地存储时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想知道,有没有办法使用Swift存储Firebase中的Time?我问的原因是因为我想要创建一个应用程序,当我创建一个特定的对象时,该对象将启动一个计时器,从创建24小时后倒计时。当这些24小时到期时,对象被删除。我还想显示一个倒数计时器,以显示对象在删除之前剩下多久。

I was just wondering, is there a means to store Time in Firebase using Swift? The reason why I ask is because I want to make an application that when I create a particular object, the object will start a timer that counts down from 24 hours of when it was created. When those 24 hours expire, the object is deleted. I also want to show a count down timer to show how much longer the object has left before it's deleted.

我尝试将Integers的小时和分钟存储到我的数据库中,但由于我不得不担心AM / PM,所以看起来效率并不高,可能还要担心关于什么日子我也在考虑将日期存储为字符串,但是在确定如何不断将字符串更改为整数并减少显示倒计时的时间方面似乎很难。

I tried storing the hour and minutes as Integers into my database but that doesn't seem very efficient since I have to worry about AM/PM, and possibly have to worry about what day it is. I was also thinking about storing the date as a string but that seems arduous in figuring out how to constantly change the string to an integer and decrement the time to show the countdown that way.

我已经查看了 FirebaseServerValue.timestamp(),但我似乎不能将其存储到Firebase 。有没有关于如何实现这一点的提示或想法?谢谢。

I've looked into the FirebaseServerValue.timestamp() but I can't seem to store that into Firebase. Are there any tips or ideas on how one would implement this? Thanks.

编辑:尝试将 FirebaseServerValue.timestamp()存入Firebase:

Attempt to store FirebaseServerValue.timestamp() into Firebase:

    self.firebaseRef.childByAutoId().setValue([
        "individualId":individualId.text,
        "timeCreated": FirebaseServerValue.timestamp()
        ])

但是我收到一条错误,说'_'不能转换为'StringLiteralConvertible'。我试图看看timestamp是否有任何方法将它变成一个String或一个整数,但是找不到任何我认为对自动完成有用的东西。

However I get an error saying that '_' is not convertible to 'StringLiteralConvertible'. I tried to see if timestamp had any methods to turn it into a String or an Integer but couldn't find anything that I thought would be useful with the autocomplete.

推荐答案

您可以使用时间间隔,只需存储一个数字

You can use time interval and just store it like a number

var interval = NSDate().timeIntervalSince1970

当你需要这个日期就这样得到它

And when you need the date just get it like this

var date = NSDate(timeIntervalSince1970: interval)

这篇关于在Firebase数据库中有效地存储时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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