Flutter is there any way to make an dynamical timestamp that print something like 10 minute ago, 1 hour agor, or 2 month ago

Issue

i already send my lastseen timestamp into firebase and how can i deduct the current timestamp with the one that i already sent to firestore. that will give me Last Seen 15 minutes ago, or maybe a long time that look like this Last Seen 20 days ago that did it. im already try to use the timeago package in pub.dev but i dont know what should i replace in the duration. the code look like this

final fifteenAgo = DateTime.now().subtract(Duration(minutes: 15));

what should i replace the minutes with ?

this is the data that i’ve been retrieve from firestore

I/flutter ( 6086): 2022-03-24 12:21:32.235192

Solution

final DateTime lastSeen = DateTime.parse(yourDataFromFireStore);
final Duration timePassed = DateTime.now().difference(lastSeen);

The timePassed variable will then hold the duration that passed since it was last seen. You will then have to define a function that takes the duration and returns the proper string that should be displayed like for example "5 days ago" or something else.

Answered By – TheUltimateOptimist

Answer Checked By – Pedro (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *