How to read Firestore timestamp in Flutter
This is how I worked it out. Import the following package:
import 'package:timeago/timeago.dart' as timeago;
Now, get the timestamp from Firestore. For example, for the field name 'timestamp', refer the following code:
final document = Firestore.instance.collection("yourCollectionName").snapshots();
Now Access your timestamp using the following:
`Timestamp timestamp = document['timestamp'];
Finally, display the result in the app. Example:
Text(timeago.format(DateTime.tryParse(timestamp.toDate().toString())).toString());
Firestore's timestamp has a toDate()
method that will return a dart DateTime
object.
From that you can use regular dart solutions, like DateFormat
or the timeago
library to display it as in:
timeago.format(firestoreTimestamp.toDate());