Firebase TIMESTAMP to date and Time

后端 未结 18 2406
温柔的废话
温柔的废话 2020-11-27 03:39

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 messa

相关标签:
18条回答
  • 2020-11-27 03:51

    For those looking for the Firebase Firestore equivalent. It's

    firebase.firestore.FieldValue.serverTimestamp()
    

    e.g.

    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);
    });
    

    Docs

    0 讨论(0)
  • 2020-11-27 03:51

    new Date(timestamp.toDate()).toUTCString()

    0 讨论(0)
  • 2020-11-27 03:53

    I converted to this format

    let timestamp = '1452488445471';
    let newDate = new Date(timestamp * 1000)
    let Hours = newDate.getHours()
    let Minutes = newDate.getMinutes()
    const HourComplete = Hours + ':' + Minutes
    let formatedTime = HourComplete
    console.log(formatedTime)
    
    0 讨论(0)
  • 2020-11-27 03:55

    timestamp is an object

    timestamp= {nanoseconds: 0,
    seconds: 1562524200}
    
    console.log(new Date(timestamp.seconds*1000))

    0 讨论(0)
  • 2020-11-27 03:57

    In fact, it only work to me when used

    firebase.database.ServerValue.TIMESTAMP
    

    With one 'database' more on namespace.

    0 讨论(0)
  • 2020-11-27 04:00

    Solution for newer versions of Firebase (after Jan 2016)

    The proper way to attach a timestamp to a database update is to attach a placeholder value in your request. In the example below Firebase will replace the createdAt property with a timestamp:

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

    According to the documentation, the value firebase.database.ServerValue.TIMESTAMP is: "A placeholder value for auto-populating the current timestamp (time since the Unix epoch, in milliseconds) by the Firebase Database servers."

    0 讨论(0)
提交回复
热议问题