how to get firebase time in firebase

1,391

Solution 1

you should use

timestamp() {
    Firestore.instance.collection('timestamp').document('time').setData({
      "timestamp": FieldValue.serverTimestamp(),
    });
  }

Solution 2

Before reading the timestamp you have to set it to the server since you are generating it from there. According to this

serverTimestamp() Returns a sentinel for use with set() or update() to include a server-generated timestamp in the written data.

so first set it

Firestore.instance.collection('timestamp').document('docId').setData({'time' : FieldValue.serverTimestamp()});

then get it using basic firestore query. Finally, check out this answer on the details of the why.

For more general way of getting timestamp

    getTimestamp() async{ 
      await Firestore.instance.collection('timestamp').document('docId').setData({'time' : FieldValue.serverTimestamp()});
      DocumentSnapshot doc = await Firestore.instance.collection('timestamp').document('docId').get();
      Timestamp timestamp = doc['time'];
      var timestampInMilliSeconds = timestamp.millisecondsSinceEpoch;
      return timestampInMilliSeconds;
      /* here you can call setState to your own variable before returning and keep in mind this method is async so it will return a Future.  */
    }
Share:
1,391
Frank van Puffelen
Author by

Frank van Puffelen

I am an engineer for Firebase at Google. I respond equally well to being called "Frank" or "puf".

Updated on December 18, 2022

Comments

  • Frank van Puffelen
    Frank van Puffelen over 1 year

    I needed in my app firebase (server) time millisecond. because my local time will be change by mobile date time setting,

    i will be see this question by not so helpful, Flutter Firestore Server side Timestamp

    var timestamp=FieldValue.serverTimestamp();
    
    print("timestamp"+timestamp.toString());
    

    but they print below value

    ====>Instance of 'FieldValue'
    
  • Admin
    Admin about 4 years
    i already use it,but this is set as per my mobile(device) time. not server.
  • Henok
    Henok about 4 years
    ok then why don't you use FiledValue.serverTimestamp() and grab the data from firestore immediately
  • Admin
    Admin about 4 years
    yes i use also this,please see my question one time more,i edited
  • Admin
    Admin about 4 years
    but i needed in separate variable.like this var timestamp=FieldValue.serverTimestamp();
  • Admin
    Admin about 4 years
    .setData({'time' : FieldValue.serverTimestamp()}); not import and can you define direct ,how i will get in timestamp in separate variable. var timeStamp="your code";
  • Henok
    Henok about 4 years
    ok check the updated answer, run it and let me know if it worked for you
  • Krishna
    Krishna about 4 years
    First thing first don't use var because it decreases readability. if you need timestamp in a variable you can assign it outsite set data object like this: timestamp = FieldValue.serverTimestamp(); and then perform whatever operation you want to perform and assign to timestamp variable for firestore like this "timestamp": timestamp,