Firebase TIMESTAMP到日期和时间

Kic*_*chu 41 javascript angularjs firebase

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

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

如果它是当前时间我只需要显示时间.我需要显示日期和时间或仅显示日期.

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

var timestamp = '1452488445471';
var myDate = new Date(timestamp*1000);
var formatedTime=myDate.toJSON();
Run Code Online (Sandbox Code Playgroud)

请为此问题提出解决方案

Bek*_*Bek 28

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

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

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

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

  • 如果你执行 `console.log(new Date(snapshot.val()))`,你会看到日期是一个字符串。`var date = new Date(snapshot.val())` 会给你一个对象中的日期,你可以在上面调用有用的函数:https://developer.mozilla.org/en-US/docs/Web/JavaScript/参考/Global_Objects/日期 (2认同)

vin*_*eet 20

时间戳是一个对象

timestamp= {nanoseconds: 0,
seconds: 1562524200}

console.log(new Date(timestamp.seconds*1000))
Run Code Online (Sandbox Code Playgroud)

  • `new Date(milliseconds)` 该函数接受毫秒,为了将秒转换为毫秒,我们乘以 1000 https://www.w3schools.com/js/js_dates.asp#:~:text=minutes%2C%20seconds%2C% 20毫秒)-,new%20Date(毫秒),-new%20Date(日期 (2认同)

Tia*_*vêa 19

事实上,它只对我有用

firebase.database.ServerValue.TIMESTAMP
Run Code Online (Sandbox Code Playgroud)

有一个'数据库'更多关于命名空间.

  • 但它使用像 1486106680368 这样的格式我如何将其转换为日期和时间 (2认同)

Dan*_*anu 19

Firebase 函数内部转换时间戳,如下所示:

timestampObj.toDate()
timestampObj.toMillis().toString()
Run Code Online (Sandbox Code Playgroud)

此处的文档https://firebase.google.com/docs/reference/js/firebase.firestore.Timestamp


Kyl*_*ley 11

对于那些寻找与Firebase Firestore等效的人.它的

firebase.firestore.FieldValue.serverTimestamp()
Run Code Online (Sandbox Code Playgroud)

例如

firebase.firestore().collection("cities").add({
    createdAt: firebase.firestore.FieldValue.serverTimestamp(),
    name: "Tokyo",
    country: "Japan"
})
.then(function(docRef) {
    console.log("Document written with ID: ", docRef.id);
})
.catch(function(error) {
    console.error("Error adding document: ", error);
});
Run Code Online (Sandbox Code Playgroud)

文件


zem*_*nkh 8

对于 Google 的新一代数据库 Firestore,以下代码将简单地帮助您解决此问题。

var admin    = require("firebase-admin");

var serviceAccount = require("../admin-sdk.json"); // auto-generated file from Google firebase.

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount)
});
var db = admin.firestore();

console.log(admin.firestore.Timestamp.now().toDate());
Run Code Online (Sandbox Code Playgroud)


Mat*_*att 6

适用于较新版Firebase的解决方案(2016年1月之后)

将时间戳附加到数据库更新的正确方法是在请求中附加占位符值.在下面的示例中,Firebase将createdAt使用时间戳替换该属性:

firebaseRef = firebase.database().ref();
firebaseRef.set({
  foo: "bar", 
  createdAt: firebase.database.ServerValue.TIMESTAMP
});
Run Code Online (Sandbox Code Playgroud)

根据文档,该值为firebase.database.ServerValue.TIMESTAMP:"Firebase数据库服务器自动填充当前时间戳(自Unix纪元以来的时间,以毫秒为单位)的占位符值."


Ser*_*kyy 6

这是一种安全方法,用于将值从 firebase Timestamp 类型转换为 JS Date,以防该值不是 Timestamp,该方法按原样返回它

适用于 Angular 7/8/9

import firebase from 'firebase';
import Timestamp = firebase.firestore.Timestamp;

export function convertTimestampToDate(timestamp: Timestamp | any): Date | any {
  return timestamp instanceof Timestamp
    ? new Timestamp(timestamp.seconds, timestamp.nanoseconds).toDate()
    : timestamp;
}
Run Code Online (Sandbox Code Playgroud)


Cod*_*ker 6

试试这个,

var timestamp = firebase.firestore.FieldValue.serverTimestamp()
var timestamp2 = new Date(timestamp.toDate()).toUTCString()
Run Code Online (Sandbox Code Playgroud)


roc*_*ift 5

使用 Firebase Firestone 18.0.1 (com.google.firebase.Timestamp)

val timestamp = (document.data["timestamp"] as Timestamp).toDate()
Run Code Online (Sandbox Code Playgroud)


小智 5

我知道 Firebase 会提供时间戳{seconds: '', and nanoseconds: ''} 以转换为您只需要做的日期:

  • 在一个 var 中获取 firebase 时间,例如:- const 日期

然后date.toDate()=> 它返回日期。