Firebase TIMESTAMP 到日期和时间 [英] Firebase TIMESTAMP to date and Time

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

问题描述

我的聊天应用程序使用了 firebase.在聊天对象中,我使用 Firebase.ServerValue.TIMESTAMP 方法添加时间戳.

I am using firebase for my chat application. In chat object I am adding time stamp using Firebase.ServerValue.TIMESTAMP method.

我需要使用此时间戳在我的聊天应用程序中显示消息接收时间.

I need to show the message received time in my chat application using this Time stamp .

如果是当前时间,我只需要显示时间.它有天差,我需要显示日期和时间或只显示日期.

if it's current time i need to show the time only.It's have days difference i need to show the date and time or only date.

我使用以下代码转换 Firebase 时间戳,但没有得到实际时间.

I used the following code for convert the Firebase time stamp but i not getting the actual time.

var timestamp = '1452488445471';
var myDate = new Date(timestamp*1000);
var formatedTime=myDate.toJSON();

请提出此问题的解决方案

Please suggest the solution for this issue

推荐答案

Firebase.ServerValue.TIMESTAMP 不是实际时间戳,它是常量,如果您将其设置为某个变量,它将被服务器中的实际值替换.

Firebase.ServerValue.TIMESTAMP is not actual timestamp it is constant that will be replaced with actual value in server if you have it set into some variable.

mySessionRef.update({ startedAt: Firebase.ServerValue.TIMESTAMP });
mySessionRef.on('value', function(snapshot){ console.log(snapshot.val()) })
//{startedAt: 1452508763895}

如果你想获得服务器时间,那么你可以使用以下代码

if you want to get server time then you can use following code

  fb.ref("/.info/serverTimeOffset").on('value', function(offset) {
    var offsetVal = offset.val() || 0;
    var serverTime = Date.now() + offsetVal;
  });

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

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