The question is how to format a Dart DateTime as a string stating the time elapsed similar to the way you see times displayed on Stack Overflow.
Is there any better method than this
String timeAgo(DateTime d) { Duration diff = DateTime.now().difference(d); if (diff.inDays > 365) return "${(diff.inDays / 365).floor()} ${(diff.inDays / 365).floor() == 1 ? "year" : "years"} ago"; if (diff.inDays > 30) return "${(diff.inDays / 30).floor()} ${(diff.inDays / 30).floor() == 1 ? "month" : "months"} ago"; if (diff.inDays > 7) return "${(diff.inDays / 7).floor()} ${(diff.inDays / 7).floor() == 1 ? "week" : "weeks"} ago"; if (diff.inDays > 0) return "${diff.inDays} ${diff.inDays == 1 ? "day" : "days"} ago"; if (diff.inHours > 0) return "${diff.inHours} ${diff.inHours == 1 ? "hour" : "hours"} ago"; if (diff.inMinutes > 0) return "${diff.inMinutes} ${diff.inMinutes == 1 ? "minute" : "minutes"} ago"; return "just now"; }
Thank you and hope it helps others
This article shows you 2 approaches to convert DateTime to time ago format in Flutter (and Dart too), for example, 5 minutes ago, 1 day ago, 2 days ago… The first one is using the difference() method provided by the DateTime class, and the second one is using a third-party plugin.
To get date time from a given timestamp, we can use the DateTime. fromMillisecondsSinceEpoch or DateTime. fromMicrosecondsSinceEpoch constructor. We have to multiply the timestamp input by 1000 because DateTime.
I used timeago for the exact purpose and found it quite useful. It has multiple format and different languages support as well.
You can do this very easily using the default intl.dart
method of Flutter
Import import 'package:intl/intl.dart';
package and get the time before date and time
So let's see how to do that, first create a TimeAgo
class
import 'package:intl/intl.dart'; class TimeAgo{ static String timeAgoSinceDate(String dateString, {bool numericDates = true}) { DateTime notificationDate = DateFormat("dd-MM-yyyy h:mma").parse(dateString); final date2 = DateTime.now(); final difference = date2.difference(notificationDate); if (difference.inDays > 8) { return dateString; } else if ((difference.inDays / 7).floor() >= 1) { return (numericDates) ? '1 week ago' : 'Last week'; } else if (difference.inDays >= 2) { return '${difference.inDays} days ago'; } else if (difference.inDays >= 1) { return (numericDates) ? '1 day ago' : 'Yesterday'; } else if (difference.inHours >= 2) { return '${difference.inHours} hours ago'; } else if (difference.inHours >= 1) { return (numericDates) ? '1 hour ago' : 'An hour ago'; } else if (difference.inMinutes >= 2) { return '${difference.inMinutes} minutes ago'; } else if (difference.inMinutes >= 1) { return (numericDates) ? '1 minute ago' : 'A minute ago'; } else if (difference.inSeconds >= 3) { return '${difference.inSeconds} seconds ago'; } else { return 'Just now'; } } }
And call it this way and get your desired output
TimeAgo.timeAgoSinceDate(item.created_date), // In this example, 09-10-2020 08:29AM date format is getting passed, but you can pass it in any format.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With