Firebase TIMESTAMP to date and Time

172,902

Solution 1

timestamp is an object

timestamp= {nanoseconds: 0,
seconds: 1562524200}

console.log(new Date(timestamp.seconds*1000))

Solution 2

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

Solution 3

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

Solution 4

In fact, it only work to me when used

firebase.database.ServerValue.TIMESTAMP

With one 'database' more on namespace.

Solution 5

inside Firebase Functions transform the timestamp like so:

timestampObj.toDate()
timestampObj.toMillis().toString()

documentation here https://firebase.google.com/docs/reference/js/firebase.firestore.Timestamp

Share:
172,902
Kichu
Author by

Kichu

Updated on July 05, 2022

Comments

  • Kichu
    Kichu almost 2 years

    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

  • Frank van Puffelen
    Frank van Puffelen over 8 years
    If you do console.log(new Date(snapshot.val())), you'll see the date as a string. var date = new Date(snapshot.val()) will give you the date in an object that you can call useful functions on: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
  • Matt Jensen
    Matt Jensen over 7 years
    Firebase.ServerValue.TIMESTAMP is a Firebase constant value not a UNIX timestamp.
  • Ayaz Ahmad Tarar
    Ayaz Ahmad Tarar over 7 years
    but it use the format like 1486106680368 how can i convert it into date and time
  • Tope
    Tope about 7 years
    the return value after setting it in the database is a UNIX timestamp but the actual command is not, so while yes at some point they resolve to the same thing and can be thusly converted, one must be careful to not try an duse the Firebase timestamp value the same way they would the getTime function in an operation.
  • Rahul Singh
    Rahul Singh almost 7 years
    the question is for js not java
  • William Wu
    William Wu almost 7 years
    @AyazAhmadTarar if you are using angular, can simply use their built in pipes such as 'Date', or use angular2-moment to easily convert the timestamp
  • Tiago Gouvêa
    Tiago Gouvêa almost 7 years
    @WilliamWu but it will use the CLIENT timestamp.. not? The firebase ServerValue.TIMESTAMP ensure that it's the "real" time from server.
  • Rahul Bagal
    Rahul Bagal over 5 years
    I am trying to use this solution however I get the following error - ReferenceError: firebase is not defined. I am using js
  • Kyle Finley
    Kyle Finley over 5 years
    Rahul, it sounds like you don't have the firebase library installed or loaded. Are you using the web version or the Node JS version? firebase.google.com/docs/firestore/quickstart
  • kai_onthereal
    kai_onthereal almost 5 years
    I found that by adding a new field name 'createdAt' to a 'timestamp' value, it automatically creates a timestamp for all documents
  • Himanshuman
    Himanshuman over 3 years
    add explanation
  • Chukwu3meka
    Chukwu3meka over 3 years
    I think this is what the user us asking
  • Aydın Candan
    Aydın Candan over 3 years
    see usage section:-)
  • Suyash Vashishtha
    Suyash Vashishtha almost 3 years
    Totally works and returns date in - Day (alphabetic ) month (alphabetic ) date (numerical) year (numerical) time (hh:mm) and Time zone (GMT+ ..). Thanks
  • Rodius
    Rodius about 2 years
    Why do you multiply seconds * 1000? Thank you
  • vineet
    vineet about 2 years
    new Date(milliseconds) the function accepts ms, to convert seconds to ms we multiply by 1000 w3schools.com/js/…