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

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

问题描述

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



我需要显示消息我的聊天应用程序收到时间使用这个时间戳。



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

我使用下面的代码来转换Firebase时间戳,但我没有得到实际的时间。

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

请为这个问题提出解决方案

解决方案

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

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







$ b

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


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.

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